Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 10 of 10

11012011, 06:55 PM #1
 Join Date
 Oct 2011
 Posts
 61
 Thanks
 3
 Thanked 0 Times in 0 Posts
Help with printing prime numbers 1100
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 1100. 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."); } }
11012011, 08:00 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,899
 Thanks
 203
 Thanked 2,531 Times in 2,509 Posts
You need to use 2 loops 
Change 11 to 100 after testing.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>
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; 11012011 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.
11012011, 08:12 PM
#3
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.
11012011, 08:14 PM
#4
 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);
11012011, 08:21 PM
#5
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.
11012011, 08:24 PM
#6
Incidentally, in place ofyou could useCode:for ( var i = 2; i < (number/2); i++ )to make even fewer "if" tests.Code:for ( var i = 2; i <= Math.sqrt(number); i++ )
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.
11012011, 08:26 PM
#7
 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!
11012011, 08:30 PM
#8
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; 11012011 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.
11012011, 08:43 PM
#9
Of course, 2 is considered prime, so maybe you could just change
toCode:list = "";
and then omit the .substring(1)Code:list = "2";
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.
11012011, 08:45 PM
#10
wth.
Added the break to exit the inner loop as soon as we find the number is not prime.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>
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.