Results 1 to 17 of 17
Thread: square root and prime numbers
 09192010, 02:59 AM #1Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
square root and prime numbers
Hello
It is the first time that I post on this site, so I do not know if it really
good category. I try to write a small java program that determines if a number is
prime or not, it looks like this:
if (nombreEntré % 2 != 0)
System.out.println(nombreEntré +" est un nombre premier"); // prime
else if(nombreEntré % 2 == 0 && nombreEntré != 2)
System.out.println(nombreEntré +" n'est pas un nombre premier"); // not prime
but if I write 51 or 25 (which are not prime number) it said me prime because it is odd. My question is what way should we change the program to detect odd numbers compounds as nonprime?
And I try to express the square root of a number, but Math.sqrt () instead of loop with if ... else or while statement. is there a way?
Sorry for my bad english
Thank you in advanceLast edited by roud9; 09192010 at 03:02 AM.
 09192010, 03:50 AM #2
For the square rooting, and you want to use a loop, you could check every number up to the number to see if it's a square root. This isn't a very good method though; Math.sqrt() is much better.
Java Code:Example: for (i = 0; i < num; i++) { if (i*i == num) return i; // If some number times itself is our number, return that. } // Note that this method will only find square roots of squares, such as 49, 64, etc.
Java Code:Example (informal psuedocode): for (i from 0 to num) { if (num % i == 0) { // % checks the remainder of a division; if it's 0, then it's evenly divisible // num is evenly divisible by i, so this number cannot be prime } }
Hope that helps a bit.
 09192010, 08:17 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Nope, for a check for primality for a number n, you need to run through every number up to sqrt(n). A bit of trickery in the forloop code itself can avoid the Math.sqrt(n) call completely:
Java Code:for (int i= 2; i*i <= n; i++) ...
kind regards,
Jos
 09192010, 11:31 AM #4Senior Member
 Join Date
 Nov 2009
 Posts
 150
 Rep Power
 11
 09192010, 12:38 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Yup, it complicates the preamble of a prime testing method a bit but it speeds up the loop:
Java Code:boolean isPrime(int n) { if (n < 2) return false; // one isn't prime by definition if (n == 2  n == 3) return true; // filter out the simple cases if (n%2 == 0  n%3 == 0) return false; // those can't be prime int inc= 2; for (int i= 5; i*i <= n; i+= inc, inc= 6inc) { // test if n can be divided by i; if so n is not prime } return true; }
kind regards,
JosLast edited by JosAH; 09192010 at 02:00 PM.
 09202010, 11:03 PM #6Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
Thanks for your reply I find what I want and how to do it. But I want a while loop instead of for loop
Java Code:for (int k = 0; k < 20; k++) racineCarrée = (racineCarrée + (nombreEntré / racineCarrée)) / 2; if (racineCarrée * racineCarrée != nombreEntré) { System.out.println("La racine entière approximative est " + racineCarrée); racineApprox ++; compteurRacines ++; } else { System.out.println("La racine entière exacte est " + racineCarrée); racineExacte ++; compteurRacines++; }
 09202010, 11:37 PM #7
A for loop is a hybrid construct of a while loop:
Java Code:a; while (b) { d; c; }
Java Code:for (a; b; c) { d; }
 09202010, 11:59 PM #8Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
Thanks :)
when I write 15, the square root give me 4 instead of 3
Java Code:racineCarrée = nombreEntré / 2; int k = 0; while (k > 20) { k ++; } racineCarrée = (racineCarrée + (nombreEntré / racineCarrée)) / 2; if (racineCarrée * racineCarrée != nombreEntré) { System.out.println("La racine entière approximative est " + racineCarrée); racineApprox ++; compteurRacines ++; } else { System.out.println("La racine entière exacte est " + racineCarrée); racineExacte ++; compteurRacines++; }
 09212010, 12:22 AM #9Senior Member
 Join Date
 Nov 2009
 Posts
 236
 Rep Power
 11
you need this:
Java Code:racineCarrée = (racineCarrée + (nombreEntré / racineCarrée)) / 2;
 09212010, 12:28 AM #10Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
The same thing give me 7 now :confused:
 09212010, 12:30 AM #11Senior Member
 Join Date
 Nov 2009
 Posts
 236
 Rep Power
 11
oh, your while loop whould be:
Java Code:k=0; while(k<20) { racineCarrée = (racineCarrée + (nombreEntré / racineCarrée)) / 2; k++; }
 09212010, 12:33 AM #12Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
Omg thanks a lot I put racineCarrée = (racineCarrée + (nombreEntré / racineCarrée)) / 2; after k ++;
 09212010, 08:27 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 09212010, 03:50 PM #14Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
because I don't need the decimal part
 09212010, 03:59 PM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 09212010, 04:01 PM #16Member
 Join Date
 Sep 2010
 Posts
 20
 Rep Power
 0
Okay thanks I will check this
 09222010, 03:20 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04012009, 02:11 PM 
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02252009, 01:21 PM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 10:09 AM
Bookmarks