I'm trying to write a JavaScript prime number generator that lists every prime between 1 and 100. I know this is a mon programming exercise and there's an abundance of solutions on the web. My question is why does my solution result an empty array? Here's the code:
var primeNumbers = [];
for (var x=2; x<101; x++)
if (x%2 === 0)
{
break;
}
else
{
for (var y=2; y<101; y++)
{
if (x/y > 1)
{
break;
}
else
{
primeNumbers.push(x);
}
}
}
};
console.log(primeNumbers);
I'm trying to write a JavaScript prime number generator that lists every prime between 1 and 100. I know this is a mon programming exercise and there's an abundance of solutions on the web. My question is why does my solution result an empty array? Here's the code:
var primeNumbers = [];
for (var x=2; x<101; x++)
if (x%2 === 0)
{
break;
}
else
{
for (var y=2; y<101; y++)
{
if (x/y > 1)
{
break;
}
else
{
primeNumbers.push(x);
}
}
}
};
console.log(primeNumbers);
Share
Improve this question
edited Mar 31, 2013 at 17:39
Lee Taylor
7,98416 gold badges37 silver badges53 bronze badges
asked Mar 31, 2013 at 17:34
Nosh BrinkenNosh Brinken
491 silver badge5 bronze badges
4
-
1
Please note how to format your code in future. Also, you've missed the opening brace after the
for
statement – Lee Taylor Commented Mar 31, 2013 at 17:41 - Plz understand the break operation.... – Ron van der Heijden Commented Mar 31, 2013 at 18:04
- @NoshBrinken - If you go to edit your question you'll see that you use 4 spaces for tabs. The use of ` is for one-off lines of code – Lee Taylor Commented Mar 31, 2013 at 18:34
- A few good prime generators can be found in prime-lib today. – vitaly-t Commented Oct 26, 2021 at 9:40
5 Answers
Reset to default 4Because the first thing you do is break if x % 2 === 0
. Which is true immediately. break
will exit the loop. I think you want continue
.
Problem 1: You break when x%2 === 0
, so you exit the loop at once. Reverse the condition and enter the code in the loop directly:
Replace this:
if (x%2 === 0) {
break;
}
else {
with:
if (x%2 !== 0) {
Problem 2: You are exiting the inner loop if x/y > 1
. This is the same as the condition x > y
, so that will always exit the inner loop immediately. Instead make the inner loop run from two and up to one less than x
:
for (var y=2; y<x; y++) {
Problem 3: Instead of dividing x
by y
and paring to one, you should use the modulo operator: x%y
. If the result is zero, then x
is not a prime number.
Problem 4: You are adding prime numbers inside the inner loop, so you will end up with most numbers multiple times, not just prime numbers once.
You need to add a variable to keep track of what's happening in the inner loop. If none of the checks in the inner loop are zero, then you can add x
to the list of prime numbers.
You don't have to test every number before the limit, you just have to test the prime numbers you found before that number. If you were looking to find all prime numbers between 1 and 10, when you are testing 7 for example you should test
7%2 === 0 false
7%3 === 0 false
7%5 === 0 false
than 7 is a prime number and your prime number array should be
[0,1,2,3,5,7]
and as you see I didn't test 4 because 4 isn't a prime number. This are the numbers you will test 8
8%2 === 0 true > return false
and you don't have to test more because you already know it isn't a prime number. So the final solution should be like
function getPrimes(x){
var ar = [];
for (var counter = 0; counter <= x; counter++) {
var notPrime = false;
for (var i = 2; i <= ar.length && !notPrime; i++) {
if (counter%ar[i]===0) {
notPrime = true;
}
}
if (notPrime === false) ar.push(counter);
}
console.log(ar)
}
getPrimes(250);
var funPrimeNumber=function(val){
var iniX=2;
var iniY=2;
var flag=1;
while(iniX<val){
iniY=2;
flag=1;
while(iniY<iniX){
if(iniX%iniY==0){
//NOT PRIME NUMBER
flag=0;
}
iniY++;
}
if(flag==1){
//PRIME NUMBER
document.write(iniX + ", ");
}
iniX++;
}
}
funPrimeNumber(100);
The issues have been listed in other answers. As this question asks about a generator and that has a specific meaning in JavaScript, let me provide a generator for primes:
function* sequence(start) { // Generator of start,start+1,start+2,...
while (true) yield start++;
}
function* sieve() { // Generator of primes using a lazy Sieve of Eratosthenes
let primes = sequence(2);
while (true) {
const prime = primes.next().value;
yield prime;
// Apply another filter to the iterator: skip multiples of latest prime:
primes = primes.filter(v => v % prime);
}
}
// Print the primes up until 100:
for (const prime of sieve()) {
if (prime > 100) break;
console.log(prime);
}