/    Sign up×
Community /Pin to ProfileBookmark

Is Prime Help

I am making a program that takes user input and then returns either true of false based on whether the number entered is prime. Prime = True, Not Prime = False. I am having trouble with the red colored code that follows. More specifically. The math is correct i think but I’m missing something.

<script type=”text/javascript”>

function frontWork() {
var n = parseFloat(document.getElementById(“inBox”).value);
var output = isPrime(n);
document.getElementById(“outBox”).value=output;
}

function isPrime(n) {
if (n == 1 || n == 2){
return true;
}

[COLOR=”Red”] for (var i=2;i<n;i++) {
if (num &#37; i == 0) {
return true;
break;

}
if (n % i != 0) {
return false;
}
else {
return true;
}
}[/COLOR]

}
</script>

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@CharlesMay 21.2009 — You've a syntax error there--and you're making things more difficult than you need.Number.prototype.isPrime = function () {
var n = Math.abs (this);
for (var i = 2; i &lt; n; i++) if (n &amp;#37; i == 0) return false;
return true;
}

n = Number (prompt ('Number', ''));
alert (n.isPrime() ? 'Prime' : 'Not prime');
Copy linkTweet thisAlerts:
@Y_LessMay 21.2009 — That can be optimised as numbers above the root of a number are only factors if a number below the root is a factor too (as they will be multiplied together):

<i>
</i>Number.prototype.isPrime = function ()
{
var
n = Math.abs(this), m = Math.sqrt(n), i = 1;
while (++i &lt;= m)
{
if (!(n &amp;#37; i))
{
return false;
}
}
return true;
}

n = Number (prompt ('Number', ''));
alert (n.isPrime() ? 'Prime' : 'Not prime');
Copy linkTweet thisAlerts:
@Declan1991May 21.2009 — And you can immediately reduce the number of modulus tests by half if you test for two separately. You can do the same trick indefinetly obviously, but it's most efficient for two IMO.<i>
</i>function isPrime(n) {
n = Math.abs(n);
var l = Math.sqrt(n);
if (n&amp;#37;2) return false;
for (i = 3; i &lt; m; i += 2) {
if (!(n % i)) {
return false;
}
}
return true;
}
Also, using closures, if generating a list of primes, you can store the primes already found in an array, and you only have to test the next number with those primes up to the square root of the number, a very efficient way to generate a list of primes. Also, you can use facts like, all primes will either be 6n + 1 or 6n - 1 to speed up the process.
Copy linkTweet thisAlerts:
@KorJan 13.2010 — Watch out the typo! :
<i>
</i>function isPrime(n) {
n = Math.abs(n);
var l = Math.sqrt(n);
if (n&amp;#37;2) return false;
for (i = 3; i &lt; [B][COLOR="red"]m[/COLOR][/B]; i += 2) {
if (!(n % i)) {
return false;
}
}
return true;
}
Copy linkTweet thisAlerts:
@Declan1991Jan 13.2010 — function primes(n) {
n = Math.abs(n);
if (n&amp;#37;2==0 || n%3==0)
return false;
for (var i = 6, l = Math.sqrt(n)+1; i &lt; l; i+=6) {
if (n%(i+1) == 0 || n%(i+1) == 0) {
return false;
}
}
return true;
}
That's the one I've been using recently. Quickest I know bar functions for generating primes.
Copy linkTweet thisAlerts:
@Y_LessJan 13.2010 — Why do you do an or with two identical conditionals?
Copy linkTweet thisAlerts:
@Declan1991Jan 13.2010 — What is with my typos in this thread!function primes(n) {
n = Math.abs(n);
if (n&amp;#37;2==0 || n%3==0)
return false;
for (var i = 6, l = Math.sqrt(n)+1; i &lt; l; i+=6) {
if (n%(i+1) == 0 || n%(i-1) == 0) {
return false;
}
}
return true;
}
Copy linkTweet thisAlerts:
@mrhooJan 13.2010 — Something's not right with Declan's code-

it returns true for the following non-prime numbers:

1, 2, 3, 25, 121, 289, 529, 841, 1681, 2209, 2809, 3481, 5041, 6889, 7921, 10201, 11449, 12769, 17161, 18769, 22201, 27889, 29929, 32041, 36481, 38809, 51529, 54289, 57121, 63001, 66049, 69169, 72361, 78961, 85849, 96721

Both of your functions call 0 and 1 prime numbers, as well as most decimals and negative numbers, which cannot by definition be prime.

I stick with Erotosthenes, who wrote this for an earlier form of javascript :

[CODE]Number.prototype.isPrime= function(){
if(this== 2) return true;
var n= Math.floor(this);
if(n!= this || this< 3 || !(this&#37;2)) return false;

var i= 3, limit= Math.sqrt(n)+1;
while(i< limit){
if(!(n%i)) return false;
i+= 2;
}
return true;
}[/CODE]

Half the code is to check for special cases- numbers less than 3 or divisible by 2 that are not 2, and non-integers.

In mathematics, a prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself

There are two conventions for the set of natural numbers: it is either the set of positive integers {1, 2, 3, ...}

according to the traditional definition or the set of non-negative integers {0, 1, 2, ...}
Copy linkTweet thisAlerts:
@Declan1991Jan 13.2010 — Ah I see my problem, I was trying prime generator trick (all primes are in the form 6n+-1) to reduce the number of division tests I had to use. Obviously it doesn't work.

And, by the way, 2 and 3 are prime.

If anyone is interested, this is the principle I mistakenly tried to implement (using JavaScript v1.8's generator functions). That definetly works because I have used it, and didn't have to alter it.function primes() {
yield 2;
yield 3;
let p = [2,3], flag;
for (let n = 6;; n+=6) {
flag = true;
let s = Math.sqrt(n)+1, t = n-1;
for (let i = 0; p[i]&lt; s; i++) {
if (t&amp;#37;p[i] == 0) {
flag = false;
break;
}
}
if (flag) {
yield t;
p[p.length] = t;
}
flag = true;
t = n+1;
for (let i = 0; p[i]&lt; s; i++) {
if (t%p[i] == 0) {
flag = false;
break;
}
}
if (flag) {
yield t;
p[p.length] = t;
}
}
}
Copy linkTweet thisAlerts:
@mrhooJan 13.2010 — It is strange that it almost worked- there are 9592 primes below 100,000 and your method correctly identified

all the intergers that are prime (yes, including 2 and 3? and only mis-called those 20 odd.
Copy linkTweet thisAlerts:
@Declan1991Jan 14.2010 — I figured it out. The problem was square numbers (in particular (6n-1)^2). Take 25. The square-root plus one is 6. The for loop then wouldn't run, which let it through. This code (or I presume using i <= l instead of <) works for sure. And this time I tested on numbers that weren't prime as well as those that are :o.function isPrime(n) {
n = Math.abs(n);
if (n == 2 || n == 3) return true;
if (n&amp;#37;2==0 || n%3==0 || n &lt; 3) return false;
for (var i = 5, l = Math.sqrt(n)+1; i &lt; l; i+=6) {
if (n%(i) == 0 || n%(i+2) == 0) {
return false;
}
}
return true;
}

I was trying to get a more efficient function, but that's probably negligible. The loop is slightly refractored, and for big numbers, it only has 2/3 of the modulus tests to do, but it has more to do initially.
Copy linkTweet thisAlerts:
@mrhooJan 14.2010 — Thank you for catching that, Declan, and explaining it- a method that works 99 percent of the time drives me nuts- I hadn't unraveled the squares yet. Nice job.
Copy linkTweet thisAlerts:
@Declan1991Jan 15.2010 — I was getting worried, I'd posted three functions in this thread, and none worked!
Copy linkTweet thisAlerts:
@KorJan 15.2010 — Here's a development (written by[B] [COLOR="Blue"]rnd me[/COLOR][/B]), following your idea:

[B]

Number.prototype.isPrime[/B]

<i>
</i>(function(){function isPrime(){ var n=this;
if(isPrime[n]){ return true; }
if( n&lt;11|| ! ((n&amp;#37;2)&amp;&amp;(n%3)&amp;&amp;(n%7)&amp;&amp;(n%9)) ){ return false; }
for (var i=5, l=Math.sqrt(n)+1; i&lt;l; i+=6){
if( !(n%i) || !(n%(i+2)) ){ return false; }
}
return isPrime.n++ &lt; 1e6 ? (isPrime[n]=true) : true;
}; var p=isPrime;p.n=0;p[2]=p[3]=p[5]=p[7]=true; Number.prototype.isPrime=p;
}());
×

Success!

Help @Jaxyral 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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