Results 1 to 7 of 7
 12132013, 09:31 PM #1Member
 Join Date
 Dec 2013
 Posts
 2
 Rep Power
 0
Prime Number Program in Java, simple program giving headache to me
hi friends,
First of all am new to programming,
while leaning some java program online i came across to this below Program.
Java Code:import java.util.*; class PrimeNumbers { public static void main(String args[]) { int n, status = 1, num = 3; Scanner in = new Scanner(System.in); System.out.println("Enter the number of prime numbers you want"); n = in.nextInt(); if (n == 1) { System.out.println("First prime number is :"); System.out.println(2); } else if (n > 1) { System.out.println("First "+n+" prime numbers are :"); System.out.println(2); } else { System.out.println("Enter the Valid Number"); } for ( int count = 2 ; count <=n ; ) { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; } } if ( status != 0 ) { System.out.println(num); count++; } status = 1; num++; } } }
Example Answer:
Enter the number of prime numbers you want
5
First 5 prime numbers are :
2
3
5
7
11
Java Code:for ( int count = 2 ; count <=n ; ) { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; } } if ( status != 0 ) { System.out.println(num); count++; } status = 1; num++; } }
how this loop code working even i tried by working the loop in paper but not getting the answer
 12132013, 10:19 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,497
 Rep Power
 9
Re: Prime Number Program in Java, simple program giving headache to me
The outer loop simply iterates until the number of required primes has been met. The inner loop simply divides the test case by numbers from 2 to the square root of the test case. If all remainders are nonzero, then the test case is a prime and the count is updated.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12142013, 09:00 AM #3Member
 Join Date
 Dec 2013
 Posts
 2
 Rep Power
 0
Re: Prime Number Program in Java, simple program giving headache to me
hi friend,
Thanks for your response
sorry to say its not clear yet (the inner loop)
Java Code:for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; }
if you have time please explain me friend
 12142013, 09:15 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,497
 Rep Power
 9
Re: Prime Number Program in Java, simple program giving headache to me
The % operator gives a remainder. So if num % j == 0 then j divides into num evenly so there is no remainder and thus num is not a prime. Once a zero remainder is detected, status is set to 0 and the loop breaks out. If, however, for the entire loop the status is never set to a 0, then the number must be a prime since no other numbers (j) divided it.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12142013, 09:39 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,321
 Blog Entries
 7
 Rep Power
 25
Re: Prime Number Program in Java, simple program giving headache to me
... and please replace that loop condition by 'j*j <= num', it's cheaper ...
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 12152013, 12:20 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,497
 Rep Power
 9
Re: Prime Number Program in Java, simple program giving headache to me
It seems that j*j is computed for each iteration of the inner loop so as num gets larger, the computation time of j*j might exceed the call of the sqrt function. Especially if the Math.sqrt(num) is hoisted out of the loop (either manually or by the compiler).
Regards,
JimLast edited by jim829; 12152013 at 01:26 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12152013, 07:40 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,321
 Blog Entries
 7
 Rep Power
 25
Re: Prime Number Program in Java, simple program giving headache to me
In the code above, Math.sqrt(num) is computed as many times as j*j would be computed; of course there are many more optimizations possbile here; b.t.w. a single multiplication can never be more expensive than a square root computation, especially when j*j <= n.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Why is my java program giving this error??
By sujskiz in forum New To JavaReplies: 5Last Post: 10062013, 05:42 PM 
Java program prime or not a prime number
By xswtnsour13x in forum New To JavaReplies: 2Last Post: 10012013, 09:21 AM 
Java imports not giving error when program ran within my Work network domain.
By pcmonto in forum New To JavaReplies: 3Last Post: 03012013, 07:16 PM 
This loop array stuff is giving me a headache
By EscSequenceAlpha in forum New To JavaReplies: 1Last Post: 05082012, 09:45 PM 
My Simple Thread program not giving proper out put ...plz help me
By nagesh1811 in forum New To JavaReplies: 4Last Post: 02272012, 07:06 PM
Bookmarks