This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by zyj1234
almost 11 years

Print all prime number between 1 to 100 by javascript

Hi, I roughly know that in order to to do that I need something like this:

function printnumbers() {
    for (i=1; i<=100; i++) {

but how to write the full codes ?

Thanks

Answer 536a815880ff33c839000e49

6 votes

Permalink

This is what I did.

for (var counter = 0; counter <= 100; counter++) {

    var notPrime = false;
    for (var i = 2; i <= counter; i++) {
        if (counter%i===0 && i!==counter) {
            notPrime = true;
        }
    }
    if (notPrime === false) {
                console.log(counter);
    }
}
points
Submitted by blacksoljah
almost 10 years

Answer 51d5ddd19c4e9d4e5a00fcea

0 votes

Permalink

//are you kidding me? all PRIME NUMBERS? i cant think of an alogorithm that could find Prime numbers which makes it kinda difficult… //the oly one i could think of is:

var PrimeNumbers=[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]; for(i=0; i<PrimeNumbers.length; i++){ console.log(“a prime number between 1 and 100 is:”+” “+PrimeNumbers[i]); }

//but you know writing them down first kind of defeats the point of the exercise //soooo…it works that way but it doesnt involve any math.

points
Submitted by Robert
over 10 years

1 comments

zyj1234 over 10 years

Thanks, I got the codes like

function checkprimenumber(n){ for (c=2; c<=n - 1; c++){ if ( n%c == 0 ){ return false; } return true; } }

function printprimenumber(number){ if(checkprimenumber(number) == true){ document.write(number+” is Prime Number
“); }else{ /document.write(number+” is Not Prime Number”);/ } }

function printnumbers(range){ for (i=1; i<=range; i++){ printprimenumber(i); } }

var maxnumber = 100; printnumbers(maxnumber);

Answer 524c6810548c357d5f0048cd

0 votes

Permalink

Hi guys.

I started to learn javascript in the last days, and this problem got my attention. It could sound like a trivial problem to solve, but is not.

If fact, deciding if a given number is a prime or not, is, indeed, a very complex maths problem, in his own right.

BUT that doesn’t meant it couldn’t be solved. Ofc you can hardcode the “solution” of what are the first 100 prime numbers and then, print the list when somebody requests it. But the thing is to build a “calculator”. Let’s say you now want the primes up to 200. ¿You will be hardcoding the primes again? hell no.

After some research, a somewhat efficent method of completing this, is to use a very well know method called Sieve of Eratosthenes wich is far less demanding that hardcoding everything, and btw, dividing every single number up to 100 between all the preceding numbers.

So far, this is my concept.

function primenum() { get all the numbers between 2 and (given number 100) point get first number in the array returns i eliminate all numbers equal to i x n+1 up to last number in the array (100) mark i as prime and eliminate from the array check if i^2 > last number in the array if yes then stop process and print stored primes if not repeat process from point }

I will learn more javascript, and then I will finish solving this problem, but so far this looks good enough, although is not the most efficent method to get the job done (sure thing there are better ones out there, but…) .

points
Submitted by Malcom Miles
over 10 years

1 comments

chri16 over 9 years

I’m doing making a program using the same concept to find prime numbers. The problem is that my program uses dynamic variables. This causes quite a few problems considering that I program javascript without a debugger and use my browser to compile it via HTML. This method is probably one of the most reliable and can be applied to any size of numbers but can get very inefficient.

Answer 53cac50c8c1cccd729001ade

0 votes

Permalink

Almost the same as blacksoljah…

//Sets up the counter (1-100 right now)
for (var counter = 1; counter <= 100; counter++)

//Creates a variable called not_prime and automatically sets it to false for each of the counter numbers 
{
    var not_prime = false;
    
//Creates a for loop with a variable called div. The loop increases by one each time until it gets to one less than the counter.
    for (var div = 2; div <= (counter - 1); div++) 
    
//If the current number divided by the "div"ider number has a remainder of 0, then it's not a prime number and not_prime is set to true.    
    {
        if (counter % div === 0) 
        {not_prime = true;}
    }
    
//If after going through all the numbers between 2 and one less than the number and none of them were evenly divided (no remainder) not_prime will still be false and the console will print the number.
    if (not_prime === false) 
    {console.log(counter);}
}
points
Submitted by Ryan
over 9 years