/    Sign up×
Community /Pin to ProfileBookmark

Prime number generator

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>
[/code]

to post a comment
HTMLJavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@awsdertauthorOct 15.2020 — Didn't like the way my code was display in my post, here's an upload instead

https://drive.google.com/file/d/1VpekFMhUeNiy4KaFOe-KEfc9P7fyQDMT/view?usp=sharing
Copy linkTweet thisAlerts:
@awsdertauthorOct 15.2020 — Never mind, just spotted the cause, forgot to change the name of a variable after copy and paste
Copy linkTweet thisAlerts:
@awsdertauthorOct 15.2020 — Nope, still broken, here's the version with the name fix done:

https://drive.google.com/file/d/1VpekFMhUeNiy4KaFOe-KEfc9P7fyQDMT/view?usp=sharing
Copy linkTweet thisAlerts:
@NogDogOct 15.2020 — I edited your original post to use this forum's ... tags. (The built-in editor's "code" button just sticks back-ticks around the text, which is fine for a bit of fixed-width text within a sentence, but not for blocks of code.)
Copy linkTweet thisAlerts:
@NogDogOct 15.2020 — I'm not much at JavaScript, but is it possible you're running into floating point arithmetic vagaries? (E.g., might it think that 49 / 7 equals 6.99999999998 or 7.0000000001?) I've at least seen that sort of thing in other languages: you can add/subtract integers as much as you want and everything is fine, but when multiplication and division enter the picture, you start getting those tiny fluctuations.)
Copy linkTweet thisAlerts:
@awsdertauthorOct 15.2020 — Nah that wasn't it, turns out I had the head cells incorrectly named and confused myself as a result, the normal method worked fine, it was the spiral code that wasn't catching all non-prime numbers, I'll re-think the code once I have a more solid idea of how to do it. Would've been nice if it worked
Copy linkTweet thisAlerts:
@rpg2009Oct 16.2020 — I don't know if this is of interest or not

[Why do we check up to the square root of a prime number to determine if it is prime?

](https://stackoverflow.com/questions/5811151/why-do-we-check-up-to-the-square-root-of-a-prime-number-to-determine-if-it-is-pr)
×

Success!

Help @awsdert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...