0

hii...any1 tell me...if i want to check prime no...then it will continue to checking untill we don't press any key...in java script...i have wrriten already code to check prime no but that is only one time not fir untill press any key to out of loop..

22nd Aug 2016, 12:21 PM
Ankita
3 Answers
+ 1
You want to add a way to stop the primality test before the end in case it's taking too long? Try this: put a button with onclick calling a function that sets a boolean to false, then in the main function initialize that bool to true before the loop, and check the bool in your loop condition (while (mybool && ...)). Other tips: - when searching factors, you can stop the loop once you reach the square root of the number you are testing - if you are testing really large numbers, it may be more interesting to use a probabilistic primality test (like Fermat or Miller-Rabin) rather than a deterministic one.
22nd Aug 2016, 12:51 PM
Zen
Zen - avatar
+ 1
First of all, your primality test should look like this, correct? function IsPrime(var n) { n = Math.abs(n); if (n < 2) return false; var f = 2; var prime = true; while (prime && (f <= Math.sqrt(n)) { prime = (n % f != 0); f++; } return prime; }
22nd Aug 2016, 1:28 PM
Zen
Zen - avatar
0
can any1 send that code...i am so confused. ..😱
22nd Aug 2016, 1:03 PM
Ankita