Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help with printing prime numbers 1-100

1. ## 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.");
}
}```

• 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

• But a better approach would be to use the Sieve of Erastothanes method.

• 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);

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

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

• Thanks, I did get it. It took a little deeper thought with your ideas, but it does work! Thanks again Old Pedant!

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

• Of course, 2 is considered prime, so maybe you could just change
Code:
`     list = "";`
to
Code:
`    list = "2";`
and then omit the .substring(1)

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

•