...

View Full Version : Help with printing prime numbers 1-100



krisdeniseriley
11-01-2011, 07:55 PM
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?
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.");
}
}

Philip M
11-01-2011, 09:00 PM
You need to use 2 loops -


<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

Old Pedant
11-01-2011, 09:12 PM
But a better approach would be to use the Sieve of Erastothanes method.

Google for it.

krisdeniseriley
11-01-2011, 09:14 PM
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);

Old Pedant
11-01-2011, 09:21 PM
I still say use the Sieve, but here's a minor mod to Philip's answer:


<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>

Old Pedant
11-01-2011, 09:24 PM
Incidentally, in place of

for ( var i = 2; i < (number/2); i++ )

you could use

for ( var i = 2; i <= Math.sqrt(number); i++ )

to make even fewer "if" tests.

krisdeniseriley
11-01-2011, 09:26 PM
Thanks, I did get it. It took a :)little deeper thought with your ideas, but it does work! Thanks again Old Pedant!

Old Pedant
11-01-2011, 09:30 PM
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:


<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>

Old Pedant
11-01-2011, 09:43 PM
Of course, 2 is considered prime, so maybe you could just change


list = "";

to


list = "2";

and then omit the .substring(1)

Old Pedant
11-01-2011, 09:45 PM
wth.


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum