sir you are such a genius...your explanation is too good very easy to understand. Thank u so much sir. sir how to develop our coding or logic ??? please suggest sir its difficult to write logic. How can I improve it???
Well explained ! Just one clarification: If we run the loop till num/2 that will also work ? bcz if any number is not divisible by half of its values it won't be divisible anyways ?
This is correct. It is an optimization. There is no sense testing values > n/2. The example for 25 will stop testing well before 12, as it will find it is not prime at 5.
/** * This program has two methods, one of which tells if a number is prime or not * and the second method returns all the list of prime numbers less than the number you've enetered */ package introduction; /** * @author saksham * */ public class PrimeNumbers { /** * @param args */ public static void main(String[] args) { int i = 23; if (isPrime(i) == true) { System.out.print( "Output 1 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are -> "); listPrimenumbers(i); System.out.println(" "); } else if (isPrime(i) == false) { System.out.print("Output 1 : Although the number you've entered (" + i + ") is not a prime number, " + "all the prime numbers below that number is/are -> "); listPrimenumbers(i); System.out.println(" "); } int j = 24; if (isPrime(j) == true) { System.out.print( "Output 2 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are -> "); listPrimenumbers(j); System.out.println(" "); } else if (isPrime(j) == false) { System.out.print("Output 2 : Although the number you've entered (" + j + ") is not a prime number, " + "all the prime numbers below that number is/are -> "); listPrimenumbers(j); System.out.println(" "); } } public static boolean isPrime(int num) { if (num 2,3,5,7,11,13,17,19,23, --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Thanks @Naveen
So that we do not have to create an object, further do not have to call that method with the object reference variable in main method. So if the method is static then we can directly call it in main method by its name.
I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented. So for example, num=25 25%2==1: fails and then i value is increased to 3 25%3==1: fails again, i=4 25%4==1: fails again, I=5 25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number. Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅
I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented. So for example, num=25 25%2==1: fails and then i value is increased to 3 25%3==1: fails again, i=4 25%4==1: fails again, I=5 25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number. Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅
perfect coding with all negative test cases
Naveen you are a maverick.
sir you are such a genius...your explanation is too good very easy to understand. Thank u so much sir.
sir how to develop our coding or logic ??? please suggest sir its difficult to write logic. How can I improve it???
Can you please make more videos on programming which will be asked for testers in interview
I tried your logic in isPrimeNumber without the for loop, I just put (if(num%2==0). i dont think I need the for loop, please clarify
Well explained ! Just one clarification: If we run the loop till num/2 that will also work ? bcz if any number is not divisible by half of its values it won't be divisible anyways ?
Just think about no. 25, it won't be divisible by 2, but divisible by 5.
This is correct. It is an optimization. There is no sense testing values > n/2. The example for 25 will stop testing well before 12, as it will find it is not prime at 5.
@@edwarddurkin6635 we can even further optimize by limiting loop till Sqrt(n) and we get same result
@Naveen AutomationLabs , again huge thanks ! Iknow u didnt use it here but still : if in isPrime() i say i
Hi Igor, please mail me at naveenanimation20@gmail.com
One question Naveen, i saw some programs on internet for checking prime numbers and there logic is including i
if you put number 9... it gives you prime and that's wrong...
Its for optimization of program. By doing i
You are the best Naveen, in all your videos that is! Popular in NYC!! Whats the best way to contact you for some offers?
Thank u so much sir for providing such good collection of videos..
Your genius 👏🏻
Y did u change it from num to i in second static method.. If it is i how it will be calling the first static method. Please clarify
best explanation, i can say ,,
Always best
Explanation 👌
Yes...u should cover
class HelloWorld {
public static boolean isPrime(int num)
{
if(num
how to print prime numbers by using scanner class?
/**
* This program has two methods, one of which tells if a number is prime or not
* and the second method returns all the list of prime numbers less than the number you've enetered
*/
package introduction;
/**
* @author saksham
*
*/
public class PrimeNumbers {
/**
* @param args
*/
public static void main(String[] args) {
int i = 23;
if (isPrime(i) == true) {
System.out.print(
"Output 1 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are ->
");
listPrimenumbers(i);
System.out.println("
");
} else if (isPrime(i) == false) {
System.out.print("Output 1 : Although the number you've entered (" + i + ") is not a prime number, "
+ "all the prime numbers below that number is/are ->
");
listPrimenumbers(i);
System.out.println("
");
}
int j = 24;
if (isPrime(j) == true) {
System.out.print(
"Output 2 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are ->
");
listPrimenumbers(j);
System.out.println("
");
} else if (isPrime(j) == false) {
System.out.print("Output 2 : Although the number you've entered (" + j + ") is not a prime number, "
+ "all the prime numbers below that number is/are ->
");
listPrimenumbers(j);
System.out.println("
");
}
}
public static boolean isPrime(int num) {
if (num
2,3,5,7,11,13,17,19,23,
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks @Naveen
I was getting an error while calling a non-static method in a static method. It seems Java doesn't support this.
Thanks thanks
Why do u declare the method as static ?
So that we do not have to create an object, further do not have to call that method with the object reference variable in main method.
So if the method is static then we can directly call it in main method by its name.
for 9 its showing true how
If i use number 9 can anyone explain
that's the same question I have, 9 is not prime but the logic accepts it
I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented.
So for example, num=25
25%2==1: fails and then i value is increased to 3
25%3==1: fails again, i=4
25%4==1: fails again, I=5
25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number.
Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅
Wrong
If we give num =15
15 % 2 == 0
1 == 0
So the condition is false
then it returns true
Can you please explain me
15%2==1 not 0. First clear your basic concepts.
@@naveenautomationlabs
How to find for number 25
I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented.
So for example, num=25
25%2==1: fails and then i value is increased to 3
25%3==1: fails again, i=4
25%4==1: fails again, I=5
25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number.
Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅
@@harinderkaur3040 thank you, that explains why it works.