This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-01-20 22:02:58

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Really fast Prime Number checker (under 9 billion)

Want to test a prime number below 92,819,543,569 (9 billion)? Just copy this javascript that I wrote... well, all except the addCommas function.  It makes a list of primes and uses it to check.  If it is taking forever to load, look for "var cont=100" and set it to a lower number; you'll have a smaller range of primes you can check, but it will load faster.  The text-boxes show you stats about the list.  Click the button to check a number.

Copy the code into Notepad on a PC or Textedit on a Mac.

IMPORTANT: As always, replace the green ) with two of these: )

P.S. My phone number is prime  smile

Update: Got rid of the green ), so just copy and paste it into Textedit/Notepad, save it as Name.html, and then open the HTML document.


<HTML>
<HEAD>
<TITLE>
Prime
</TITLE>
<SCRIPT TYPE="text/javascript">

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}



var PrimeList = new Array();

function primelist(n)
{
    PrimeList = [2,3,5]

    var test=6;
    var i=0;
    var sq=3;
    var k;
    var add;
    var cont=100;
    var sqt=1;

    for(i=0; i<cont; i++)
    {
        for(test=test; test<(PrimeList[i+1]*PrimeList[i+2]); test++)
        {
            add = true
            for(k=0; k<=i; k++)
            {
                if(test%PrimeList[k]==0)
                {
                    add=false;
                }
            }
            if(add)
            {
                if(sq*sq==test)
                {
                    sqt++;
                    sq=PrimeList[sqt]
                }
                else
                {
                    PrimeList.push(test);
                }
            }
        }
    }
return PrimeList
}

function pr (n)
{
if(n==1 || n==0)
{
    alert(n+" is not prime")
}
else
{
    var i;
    var j = Math.sqrt(n);
    var k = true;

    for(i=0; PrimeList[i]<=j; i++)
    {
        if(n%PrimeList[i]==0)
        {
            k=false;
            break;
        }
    }
    if(k)
    {
        alert("Prime")
    }
    else
    {
        alert(n +" is not prime, it is divisible by "+ PrimeList[i])
    }
}
}
</SCRIPT>
</HEAD>
<BODY onLoad="document.na.prime.value=primelist(); document.na.len.value=PrimeList.length; document.na.hig.value=PrimeList[PrimeList.length-1]; alert('Loaded');">
<form name="na">
List:<input type="text" size="100" name="prime"><BR>
Length:<input type="text" size="6" name="len"><BR>
Last Item:<input type="text" size="6" name="hig"><BR>
<input type="button" value="Check Number" onClick="var mm = document.na.hig.value; pr(prompt('Number > 0 and < '+(addCommas(mm*mm) ) ) );">
</form>
</BODY>
</HMTL>

Last edited by MoreGamesNow (2011-01-31 16:50:45)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

Board footer