Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    61
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help with printing prime numbers 1-100

    I have the following code attached that gives me a prime number. I can't seem to put the logic behind having this code print from an alert the prime numbers from 1-100. Of course, this code is only giving me "the" prime number. I just don't know where to begin, or how to store each number. Array maybe?
    Code:
    var finding_primes = function() {
    	var number = 31, prime = true;
    	for ( var i = 2; i < number; i++ ) {
    		if ( number % i == 0 ) prime = false;
    	}
    	if (prime) {
    		alert( number + " is prime.");
    	} else {
    		alert( number + " is not prime.");
    	}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You need to use 2 loops -

    Code:
    <script type = "text/javascript">
    function finding_primes() {
    for (var number = 2; number<=11; number++) {
    var prime = true;  // set prime to true for each number in turn
    for ( var i = 2; i < (number/2); i++ )  // number/2 is enough!
    if ( number % i == 0 ) {prime = false}
    }
    if (prime) {
    alert ( number + " is prime.");
    } 
    else {
    alert ( number + " is not prime.");
    }
    }
    }
    
    finding_primes();
    
    </script>
    Change 11 to 100 after testing.


    Alerts are a rather primitive way of outputting data. You could add the result of each test and <br> to a variable named "result" and then display this variable using document.getElementById("fieldname").innerHTML = result;


    If there is a goal now, it is really going to count for the side that scores it. - Football Commentator, BBC Radio London
    Last edited by Philip M; 11-01-2011 at 08:07 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    But a better approach would be to use the Sieve of Erastothanes method.

    Google for it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    61
    Thanks
    3
    Thanked 0 Times in 0 Posts
    How would I go about making all the prime factors between 1 and 100 print out all at once in an "alert" ? I don't want them to show 1 prime at a time.

    example
    alert("The prime numbers between 1 and 100 are: " +foo);

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    I still say use the Sieve, but here's a minor mod to Philip's answer:
    Code:
    <script type = "text/javascript">
    
    function finding_primes() 
    {
        var list = "";
        for (var number = 2; number<=100; number++) 
        {
            var prime = true;  // set prime to true for each number in turn
            for ( var i = 2; i < (number/2); i++ ) 
            {
                if ( number % i == 0 ) {prime = false}
            }
            if (prime) { list += "," + number; } // tack on the number if it is prime
       }
       alert ( "primes: " + list.substring(1) ); // substring chops off the first comma
    }
    
    finding_primes();
    
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Incidentally, in place of
    Code:
            for ( var i = 2; i < (number/2); i++ )
    you could use
    Code:
            for ( var i = 2; i <= Math.sqrt(number); i++ )
    to make even fewer "if" tests.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Oct 2011
    Posts
    61
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks, I did get it. It took a little deeper thought with your ideas, but it does work! Thanks again Old Pedant!

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Actually, there's a VERY minor change that makes the algorithm TWICE as efficient!

    Simply reallize that *NO* even number can ever be prime, and it's obvious:
    Code:
    <script type = "text/javascript">
    
    function finding_primes() 
    {
        var list = "";
        // start on first odd prime, then only check odd numbers!
        for ( var number = 3; number<=100; number += 2 ) 
        {
            var prime = true;  // set prime to true for each number in turn
            // similarly, no prime number can ever have an EVEN factor!!!
            for ( var i = 3; i <= Math.sqrt(number);  i += 2 ) 
            {
                if ( number % i == 0 ) {prime = false}
            }
            if (prime) { list += "," + number; } // tack on the number if it is prime
       }
       alert ( "primes: " + list.substring(1) ); // substring chops off the first comma
    }
    
    finding_primes();
    
    </script>
    Last edited by Old Pedant; 11-01-2011 at 08:35 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Of course, 2 is considered prime, so maybe you could just change
    Code:
         list = "";
    to
    Code:
        list = "2";
    and then omit the .substring(1)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    wth.
    Code:
    <script type = "text/javascript">
    
    function finding_primes() 
    {
        var list = "2";  // 2 is prime, so let's just give that as starting point
    
        // start on first odd prime, then only check odd numbers!
        for ( var number = 3; number<=100; number += 2 ) 
        {
            var prime = true;  // set prime to true for each number in turn
            // similarly, no prime number can ever have an EVEN factor!!!
            for ( var i = 3; i <= Math.sqrt(number);  i += 2 ) 
            {
                if ( number % i == 0 ) {prime = false; break; } // stop as soon as you get false
            }
            if (prime) { list += "," + number; } // tack on the number if it is prime
       }
       alert ( "primes: " + list );
    }
    
    finding_primes();
    
    </script>
    Added the break to exit the inner loop as soon as we find the number is not prime.

    I think this is as far as it is worth taking this algorithm.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •