Reputation: 49
I'm trying to write a JavaScript prime number generator that lists every prime between 1 and 100. I know this is a common 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);
Upvotes: 4
Views: 5607
Reputation: 350961
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);
}
Upvotes: 0
Reputation: 1
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);
Upvotes: 0
Reputation: 11
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);
Upvotes: 1
Reputation: 700680
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 comparing 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.
Upvotes: 2
Reputation: 120278
Because 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
.
Upvotes: 4