Saw a vid on how prime numbers can look like a spiral when plotted to x,y axis, decided to see if I could make a generator based on quirks noticed as a result of that, the generator appears to work but the one I made in addition to check against seems to be buggy (gave 49 as a prime number despite it being a multiple of 7 which is pushed into the array of numbers checked against), someone mind taking a look to see if they can fix it, I’m not seeing the cause after all
[code]<!doctype html>
<html>
<head>
<script>
// <!–[CDATA[
function id(ID) { return document.getElementById(ID); }
function prime_spiral( from, upto )
{
var rows = [];
for ( ; from < upto; ++from )
{
if ( from < 6 )
{
if ( from === 1 || from === 4 )
continue;
rows.push(from);
}
else if ( (from – 1) % 6 === 0 && from % 5 !== 0 )
{
rows.push(from);
}
else if ( (from – 5) % 6 === 0 && from % 5 !== 0 )
{
rows.push(from);
}
}
return rows;
}
function prime_normal( from, upto )
{
var all = [], rows = [], i, j;
for ( i = 2; i < from; ++i )
{
for ( j = 0; j < rows.length; ++j )
{
if ( i % all[j] === 0 )
break;
}
if ( j === all.length )
all.push(i);
}
for ( ; i < upto; ++i )
{
for ( j = 0; j < rows.length; ++j )
{
if ( i % all[j] === 0 )
break;
}
if ( j === all.length )
{
all.push(i);
rows.push(i);
}
}
return rows;
}
function prime(name)
{
var from = id(“from”), upto = id(“upto”), out = id(“out”);
var ml = “”, srows = [], nrows = [], i;
from = parseInt(from.value);
upto = parseInt(upto.value);
if ( name === “spiral” )
{
ml = “prime_spiral( ” + from + “, ” + upto + ” )”;
srows = prime_spiral( from, upto );
}
else if ( name === “normal” )
{
ml = “prime_normal( ” + from + “, ” + upto + ” )”;
nrows = prime_normal( from, upto );
}
else
{
ml = “prime_compare( ” + from + “, ” + upto + ” )”;
srows = prime_spiral( from, upto );
nrows = prime_normal( from, upto );
}
console.log(ml);
ml = “<tr><th>Spiral</th><th>Normal</th></tr>”;
for ( i = 0; i < nrows.length; ++i )
{
ml += “<tr><td>” + nrows[i] + “</td><td>”;
if ( typeof srows[i] === “number” )
ml += srows[i];
ml += “</td></tr>”;
}
for ( ; i < srows.length; ++i )
{
ml += “<tr><td></td><td>” + srows[i] + “</td></tr>”;
}
out.innerHTML = ml;
}
// ]]–>
</script>
</head>
<body>
<form action=”#”>
<label>From: <input id=”from” /></label>
<label>Upto: <input id=”upto” /></label>
<button onclick=”prime(‘spiral’)”>Spiral</button>
<button onclick=”prime(‘normal’)”>Normal</button>
<button onclick=”prime(‘compare’)”>Compare</button>
</form>
<table id=”out”></table>
</body>
</html>