public
Created

Javascript prime number generator

  • Download Gist
prime.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
<!DOCTYPE html>
<html>
<head>
<title>JS Prime Generator</title>
</head>
<body>
<div id="res"></div>
<button id="generate">Generate more primes!</button>
<script type="text/javascript">
(function(){
"use strict";
//Add event listener to the button
document.getElementById("generate").addEventListener("click", get_primes, false);
var timeDiff = {
setStartTime:function ()
{
var d = new Date();
this.time = d.getTime();
},
getDiff:function ()
{
var d2 = new Date();
return (d2.getTime()-this.time);
}
};
function get_primes()
{
//Start the timer
timeDiff.setStartTime();
//Define primes array
var primes = [];
//Check for "primes" key in localstorage
if(localStorage.getItem("primes") != null)
{
var existing_primes = JSON.parse(localStorage.getItem("primes"));
var num_primes = existing_primes.length;
// If there is a "primes" key in localstorage, add it to the
// primes array
for(var i=0;i< num_primes;i++)
{
primes.push(existing_primes[i]);
}
}
else //Seed the prime generator with the first prime number
{
primes.push(2);
}
// Get the largest existing prime, and add one
// to make it the starting number in the search for the next prime
var start_num = primes[primes.length - 1] + 1;
// Specify a sane ending point
var end_num = 999 + start_num;
// Speed it up a tad
if(start_num % 2 === 0)
{
start_num++;
end_num++;
}
// Flag for determining if a number is prime
var is_prime = true;
var x = 0;
for(i = start_num;i < end_num;i+=2)
{
is_prime = true;
num_primes = primes.length;
for(x=0;x<num_primes;x++)
{
//If the remainder is 0, the number is a composite
if(i % primes[x] === 0)
{
is_prime = false;
break;
}
}
// If the number is prime, add it to the primes array
if(is_prime === true)
{
primes.push(i);
}
}
//Save the array in localStorage for the next round(s)
localStorage.setItem("primes", JSON.stringify(primes));
var output = "";
//String together all the primes
output += primes.join(", ");
output += '<br /><br />';
var total_time = timeDiff.getDiff();
output += 'Generated '+primes.length+' primes in '+(total_time / 1000)+' seconds.';
document.getElementById('res').innerHTML = output;
location.href="#generate";
}
}());
</script>
<button onclick="localStorage.setItem('primes', '[2]')">Start Over</button>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.