Finding all primes less than input

I have input fieldto take the maximum number and find all smaller primes .

It should return to arrayand display arraywith alert.

In addition, I get empty every time.

HTML:

<p>Please enter the maximum number you'd like to find all prime numbers below it for.</p>
<p><input type="text" id="number" /></p>
<button id="run">RUN</button>

CSS

#go, #number {float:left;}

JavaScript:

var primes=[];
function isPrime(x){
    var prime=true;
    for(var i=0;i<=Math.sqrt(x);i++){
        if(x%i===0){
            prime=false;
        }
    }
    if(prime){
        primes.push(x);
    }
};
$('#run').on('click',function(){
    var total=$('#number').val();
    for(var j=2;j<=total;j++){
        isPrime(j);
    }
    alert(primes);
});

http://jsfiddle.net/jH5jq/1/

+4
source share
2 answers

This was the problem:

for(var i=0;i<=Math.sqrt(x);i++){

Of course, he will be divided into 1.

Revised and working jsfiddle

JavaScript:

var primes=[];
function isPrime(x){
    var prime=true;
    for(var i=2;i<=Math.sqrt(x);i++){
        if(x%i===0){
            prime=false;
        }
    }
    if(prime){
        primes.push(x);
    }
};
$('#run').on('click',function(){
    var total=$('#number').val();
    for(var j=3;j<=total;j++){
        isPrime(j);
    }
    alert(primes);
    primes=[];
});
+1
source

There are several improvements that can and should be made in the code that was originally published:

:

  • -, "isPrime (x)",

, ,

  • ,
  • ,
  • , .
  • , .

HTML - div,

<p>Please enter the maximum number you'd like to find all prime numbers below it for.</p>
<p><input type="text" id="number" /></p>
<button id="run">RUN</button>
<br/>
<div id="answer"></div>

JS - isPrime boolean, findPrimes ,

// try to grab our prime list out of browser localstorage
try {
    window.primes = JSON.parse(window.localStorage["primes"]);
    console.log("retrieved "+window.primes.length+" primes from storage");
    // print the seed primes to the console in case of bug
    console.log(window.primes); 
} catch(e){};

// seed it with a few primes if empty
if (typeof(window.primes)!=="object") window.primes=[2,3,5,7];  

function isPrime(x){
    // isPrime takes x and returns a Boolean if x is Prime
    var prime=false, i=0,l=primes.length;
    var maxprime=primes[l-1];
    var reqprime = Math.floor(Math.sqrt(x));
    if (reqprime>maxprime) {
      findPrimes(reqprime);
      // the primes list has changed, set l again
      l = primes.length;
    }
    while( (i<l) && (x%primes[i]!==0) && (primes[i]<=reqprime) ) ++i;
    prime = ( (i===l)  || (primes[i]>reqprime) ); 
    // if i is l then x is prime
    // if we have checked all the primes up to sqrt(x) then x is prime
    return prime
};

function findPrimes(x){
    // findPrimes finds new primes up to and including x
    // returns an Array of prime numbers
    var i=0,result=[],l=primes.length;
    var maxprime=primes[l-1];
    if (x>maxprime){ 
        for(i=maxprime+2; i<=x; i+=2){
            if (isPrime(i)) primes.push(i);
        }
        l=primes.length;
        // try to set browser localstorage with new list of Primes
        // fail with console message only
        try {
            window.localStorage["primes"]=JSON.stringify(primes);
        } catch(e){ console.log("cant set primes in localStorage"); };
    }
    i=0;
    while( (i<l) && (primes[i]<=x)){
        result.push(primes[i]);
        ++i;
    }
    return result;
}

$('#run').on('click',function(){
    var total=$('#number').val();
    var answer = findPrimes(total);
    $('#answer').html("<ul><li>"+
                      answer.join("</li><li>")+
                      "</li></ul>"
                      );
});

JSFIDDLE

0

Source: https://habr.com/ru/post/1542797/


All Articles