Determining wether a user's number is Prime?

Discussion in 'JavaScript' started by pHrEaK, Feb 17, 2011.

  1. #1
    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!
     
    pHrEaK, Feb 17, 2011 IP
  2. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    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?
     
    pHrEaK, Feb 18, 2011 IP
  3. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    anyone? :confused:
     
    pHrEaK, Feb 19, 2011 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    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
     
    Last edited: Feb 20, 2011
    JEET, Feb 20, 2011 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    <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...
     
    Last edited: Feb 21, 2011
    camjohnson95, Feb 21, 2011 IP
  6. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    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:?
     
    pHrEaK, Feb 21, 2011 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    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):
     
    camjohnson95, Feb 21, 2011 IP
  8. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    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...
     
    camjohnson95, Feb 21, 2011 IP
  9. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Thank you a ton, that works, and i see now where my error was. Greatly appreciated!
     
    pHrEaK, Feb 21, 2011 IP