Been trying to figure this out all day and I've gotten no where. What I'm trying to accomplish is to take a user's number and determine whether or not it's prime. I know this involves the mod operator some way, but i just cannot figure out the formula for this. I'm not asking anyone to do this for me, I've just been racking my brain all day trying to understand how to do this and I've made little to no progress. ANY insight on this is GREATLY appreciated!
Alright, i figured out the logic behind determining whether the number is prime. Basically you take the input number and modulus divide it by every number up until the input number itself and if it returns zero you know the number is not prime. I've started on a script and something doesn't seem to be working out right. Got something mixed up somewhere: <script type="text/javascript"> /* <![CDATA[ */ function wLoop(input){ var check=2; var isPrime = true; input = parseInt(input); while (check < input){ if(input % check==0) { isPrime = false; } else { isPrime = true; } if(isPrime = false) { window.alert("Number is not prime"); break; } else { window.alert("Number is prime"); break; } check++ } } /* ]]> */ </script> Code (markup): Anyone see where i may have made an error?
Try this. I haven't tested it: <script type="text/javascript"> function wLoop(input){ var check=2; var isPrime = true; input = parseInt(input); for(check<=2;check<input;++check){ if(isPrime==true){ if(input % check==0){ isPrime = false; } } } return isPrime; } n=4; if(wLoop(n)==true){ alert(" is prime "); }else{ alert(" is not prime "); } </script> Code (markup): If you have problem copying, go here: http://websitedays.com/tforum_1.29.0_javascript-check-if-number-is-prime.html Thanks
<script type="text/javascript"> function isPrime(num) { if((num % 2) == 0) return false; //speed things up for larger nums for(var i=3;i<num;i++) if((num % i) == 0) return false; return true; } //list prime nums to 100 for(var i = 0; i<100; i++) if(isPrime(i)) document.writeln(i); </script> Code (markup): continuing to search once the number has been divisible is overkill.. just return false and exit function once it has been, otherwise return true...
Tried a hybrid of both and came up with this: <script type="text/javascript"> /* <![CDATA[ */ function wLoop(num) { num = parseInt(num); for(var i=2;i<num;i++) if((num % i) == 0) { x = false; } else x = true; if(x==false) window.alert("number is not prime"); if(x==true) window.alert("number is prime"); } /* ]]> */ </script> Code (markup): Now no matter what is entered it always displays "number is prime" :scratches head:?
Because your loop is continuing to execute until it reaches the number before the user's inputted number it is always returning a true value (number is prime). So if the user enters 26, then last code to execute is: if((26 % 25) == 0) { x = false; } else { x = true; } Code (markup): If you want to use your code the easiest way to do this is to force an exit of the loop once it has determined the number is not a prime. e.g: function wLoop(num) { num = parseInt(num); for(var i=2;i<num;i++) { if((num % i) == 0) { x = false; i = num; // <<force exit of loop } else { x = true; } } if(x==false) window.alert("number is not prime"); if(x==true) window.alert("number is prime"); } Code (markup):
On second thoughts the more logical option would be to set the value of x to true by default and set it to false if the number is divisible: function wLoop(num) { num = parseInt(num); var x = true; for(var i=2;i<num;i++) { if((num % i) == 0) x = false; } if(x==false) window.alert("number is not prime"); if(x==true) window.alert("number is prime"); } Code (markup): There are a million ways this code could be written...