RufioLJ
RufioLJ

Reputation: 437

Count the prime numbers from 2 to 100 with simpler code than this

It has to be with just functions, variables, loops, etc (Basic stuff). I'm having trouble coming up with the code from scratch from what I've I learned so far(Should be able to do it). Makes me really mad :/. If you could give me step by step to make sure I understand I'd really really appreciated. Thanks a bunch in advanced.

How could I get the same result with a simpler code than this one:

var primes=4; 
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    var isPrime = 0;
    if(isPrime === 0){ 
        if(counter === 2){console.log(counter);} 
        else if(counter === 3){console.log(counter);} 
        else if(counter === 5){console.log(counter);} 
        else if(counter === 7){console.log(counter);} 
        else if(counter % 2 === 0){isPrime=0;} 
        else if(counter % 3 === 0){isPrime=0;} 
        else if(counter % 5 === 0){isPrime=0;} 
        else if(counter % 7 === 0){isPrime=0;}
        else {
            console.log(counter);
            primes = primes + 1;
        }
    }
}
console.log("Counted: "+primes+" primes");

Upvotes: 0

Views: 5944

Answers (6)

Will Ness
Will Ness

Reputation: 71119

This is pretty simple (I mean, short):

console.log(2); console.log(3);
var m5=25, m7=49, i=5, d=2, c=2;
for( ; i<100; i+=d, d=6-d )
{
    if( i!=m5 && i!=m7) { console.log(i); c+=1; }
    if( m5 <= i ) m5+=10;
    if( m7 <= i ) m7+=14;
}
c

It is an "unrolled" sieve of Eratosthenes with 2-3-wheel factorization.

On the one hand, we enumerate all the numbers below 100 (and bigger than 3) that are coprime with 2 and 3, as partial sums of 5+2+4+2+4+... , so that there are no multiples of 2 and 3 among thus enumerated numbers.

On the other hand, we discard from among them all the multiples of 5 and 7, by enumerating them as partial sums of 25+10+10+10+... and 49+14+14+14+... . Multiples of 2 and 3 are not there in the first place, and the first multiple of 11 that needs to be discarded by the sieve of Eratosthenes is 121.

Upvotes: 0

Aran Mulholland
Aran Mulholland

Reputation: 23945

I'm feeling naughty today so:

function printPrimesBetweenTwoAndOneHundredSimply(){

   var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
   i,
   arrayLength = primes.length;

   for(i = 0; i < arrayLength; i++){
     console.log(primes[i]);
   }

   console.log("Counted: " + arrayLength + " primes");
}

Upvotes: 7

This will be simpler for your Question....

 for (int i = 2; i < 100; i++) {
int j = 0;
for (j = 2; j < i; j++)
    if ((i % j) == 0)break;
        if (i == j)
        System.out.print("  " + i);}

Upvotes: -3

RobG
RobG

Reputation: 147483

Of course "simpler" is not defined. :-)

The following can be made much shorter, but has a bit of robustness built in.

// Get primes from 0 to n. 
// n must be < 2^53
function primeSieve(n) {

  n = Number(n);
  if (n > Math.pow(2, 53) || isNaN(n) || n < 1) {
    return;
  }

  var primes = [];
  var notPrimes = {};
  var num;

  for (var i=2; i<n; i++) {
    for (var j=2; j<n/2; j++) {
      num = i*j;
      notPrimes[num] = num;
    }
    if (!(i in notPrimes)) {
      primes.push(i);
    }
  }
  return primes
} 

So if you want less code:

// Get primes from 0 to n. 
// n must be < 2^53
function primeSieve2(n) {
  var primes = [], notPrimes = {};
  for (var i=2; i<n; i++) { 
    for (var j=2; j<n/2; j++)
      notPrimes[i*j] = i*j;
    i in notPrimes? 0 : primes.push(i);
  }
  return primes
}

Upvotes: 0

MiniGod
MiniGod

Reputation: 3802

This finds all prime numbers between 2 and 100:

var primes=0; 
var isprime = true;
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    // For now, we believe that it is a prime
    isprime = true;
    var limit = Math.round(Math.sqrt(counter)); // See comment from @AresAvatar, below
    // We try to find a number between 2 and limit that gives us a reminder of 0
    for (var mod = 2; mod <= limit; mod++) {
        // If we find one, we know it's not a prime
        if (counter%mod == 0) {
            isprime = false;
            break; // Break out of the inner for loop
        }
    }

    if (isprime) {
        console.log(counter, limit);
        primes = primes + 1;
    }
}
console.log("Counted: "+primes+" primes");

Upvotes: 1

Rhyono
Rhyono

Reputation: 2468

First, you really don't need to use === for this. The standard == will suffice. Second, you can make all of those lines that are the same thing, except for a single digit into one line:

var primes=4; 
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    var isPrime = 0;
    if(isPrime == 0){ 
        if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
        else if(counter % 2 == 0 || counter % 3 == 0 || counter % 5 == 0 || counter % 7 == 0)isPrime=0; 
        else {
            console.log(counter);
            primes = primes + 1;
        }
    }
}
console.log("Counted: "+primes+" primes");

You'll also notice that the {} was removed on a few lines. This is because a single line of code following an if (among others) is always considered nested.

Next, we can change your primes = primes + 1; to this: primes++; which just tells primes to increment itself by one. The same can be done for your counter. We also know that isPrime equals "0" because you set it to that a second ago, so we no longer need that if statement:

var primes=4; 
for (var counter = 2; counter <= 100; counter++)
{
    var isPrime = 0;
    if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
    else if(counter % 2 == 0 || counter % 3 == 0 || counter % 5 == 0 || counter % 7 == 0)isPrime=0; 
    else {
        console.log(counter);
        primes++;
    }
}
console.log("Counted: "+primes+" primes");

Next, we can do a negative check (!= instead of ==) on the values and combine your else if with your else. Since we're doing a negative check (for this case) we have to switch the ORs (||) to ANDs (&&):

var primes=4; 
for (var counter = 2; counter <= 100; counter++)
{
    if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
    else if(counter % 2 != 0 && counter % 3 != 0 && counter % 5 != 0 && counter % 7 != 0) {
        console.log(counter);
        primes++;
    }
}
console.log("Counted: "+primes+" primes");

There are many other ways to write it, but I felt it more beneficial to you to use what you started with and shorten it from there.

Upvotes: 4

Related Questions