patternjavascriptMinor
First attempt at generating prime numbers
Viewed 0 times
numbersattemptgeneratingfirstprime
Problem
I had to look up some other solutions online because I could not figure it out on my own. I really wish I could have come up with it completely on my own, but that didn't happen. Nevertheless, please review my prime number program and let me know if there are any improvements I should make or advice your would like to give me about it.
var primes = []; // will become a list of prime numbers
primes_loop:
for (var n = 2; n < 10; n++) {
if (n === 2) {
primes.push(n); // first prime number is stored
continue primes_loop; // continue iteration of the loop
}
divisors_loop:
for (var i = 2; i < n; i++) {
if (n % i === 0) {
break divisors_loop; // n is not prime if condition is true
}
else {
primes.push(n); // update prime list with the prime number
}
}
}
for (var index = 0; index < primes.length; index++) {
console.log(primes[index]);
}Solution
First of all, there's a slight problem with your code. I get this output:
So it repeats quite a bit, and -- wait, 9 is not a prime number. Oops.
For, say, numbers up to 25, you'd get 21 instances of
The way your code runs, you don't need the labels (
However, a label might be handy for a revised version that actually does find primes and only primes (of course, there are other, smarter approaches to all of this, but I'm just sticking to the code that was posted):
After that, you'll be left with a
No repetition, and no false positives.
Of course, it'd be nicer to wrap as a function:
2, 3, 5, 5, 5, 7, 7, 7, 7, 7, 9So it repeats quite a bit, and -- wait, 9 is not a prime number. Oops.
For, say, numbers up to 25, you'd get 21 instances of
23 in your array. And we'd get a 24 too, although that's not prime. We'll deal with that in a second. The way your code runs, you don't need the labels (
primes_loop and divisors_loop); when you continue or break you do so for the "closest" loop you're in. So simply writing continue with no label will skip to the next iteration of the outer (prime) loop, and break will break the inner (divisor) loop.However, a label might be handy for a revised version that actually does find primes and only primes (of course, there are other, smarter approaches to all of this, but I'm just sticking to the code that was posted):
var primes = [],
limit = 10,
n, divisor;
outerLoop: for( n = 2 ; n <= limit ; n++ ) {
for( divisor = 2 ; divisor < n ; divisor++ ) {
if( n % divisor === 0 ) {
continue outerLoop; // not a prime, try the next n
}
}
primes.push(n); // if we made it all the way here, then n is prime
}After that, you'll be left with a
primes array that looks like this:[ 2, 3, 5, 7 ]No repetition, and no false positives.
Of course, it'd be nicer to wrap as a function:
function findPrimes(limit) {
var primes = [], n, divisor;
outerLoop: for( n = 2 ; n <= limit ; n++ ) {
for( divisor = 2 ; divisor < n ; divisor++ ) {
if( n % divisor === 0 ) {
continue outerLoop;
}
}
primes.push(n);
}
return primes;
}Code Snippets
2, 3, 5, 5, 5, 7, 7, 7, 7, 7, 9var primes = [],
limit = 10,
n, divisor;
outerLoop: for( n = 2 ; n <= limit ; n++ ) {
for( divisor = 2 ; divisor < n ; divisor++ ) {
if( n % divisor === 0 ) {
continue outerLoop; // not a prime, try the next n
}
}
primes.push(n); // if we made it all the way here, then n is prime
}[ 2, 3, 5, 7 ]function findPrimes(limit) {
var primes = [], n, divisor;
outerLoop: for( n = 2 ; n <= limit ; n++ ) {
for( divisor = 2 ; divisor < n ; divisor++ ) {
if( n % divisor === 0 ) {
continue outerLoop;
}
}
primes.push(n);
}
return primes;
}Context
StackExchange Code Review Q#45431, answer score: 6
Revisions (0)
No revisions yet.