Forum Moderators: open

Message Too Old, No Replies

Javascript Prime Numbers

Help!

         

TomJS

7:03 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I need to write a Javascript that finds all prime numbers below 1000, can anybody help please? i am a beginner.

ajkimoto

10:42 pm on Feb 27, 2004 (gmt 0)

10+ Year Member


tomJS,

How about this:

//check from 1 to 1000
var primeCount=1000

//initialize empty string to hold prime numbers
var primeString=''

function checkPrime(){

//cycle through numbers from 1 to 1000
for(i=1; i<1000; i++){

//initialize isPrime to 1 each time the i loop runs
varisPrime=1

//cycle through factors
for (j=2; j<i; j++){
//if divisible by factor, set isPrime to 0
if(i%j==0){
isPrime=0
//exit loop
j=i
}
}
//add a <br /> plus i if i is prime
primeString=(isPrime==1?primeString+'<br />'+i:primeString)
}

//after cycling through all numers, replace the contents of a div with
// id="primeDiv" with primeString
document.getElementById('primeDiv').innerHTML=primeString

}

Now all you need is a div with id="primeDiv", and put the function checkPrime in the onload event of th e body tag, and you should have it:

<body onload="checkPrime()">
<h2>The following numbers are prime</h2>

<div id="primeDiv">Prime numbers will be displayed here shortly...</div>
</body>

Hope this helps,

ajkimoto

TomJS

9:42 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Ah this is good, thanks very much but I do not completely understand a couple of lines, specifically the i=j line I don't get how that ends the loop. I also don't quite get the j++ and i++ in the for loops.
Could you also please elaborate on these two lines and how they work?
primeString=(isPrime==1?primeString+'<br />'+i:primeString)
document.getElementById('primeDiv').innerHTML=primeString

TomJS

2:59 am on Mar 4, 2004 (gmt 0)

10+ Year Member



OK i rewrote it in my own way and it works but i don't understand one part. How come you have to say primeString=primeString+"<br>"+i

Purple Martin

4:47 am on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Inserting a <br> into the string puts a soft-return (line break) in the output, so you don't get it all on one line.

PCInk

10:12 am on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



for (j=2; j<i; j++)

A quicker method, is to check from j=2 to j<=sqr(i), assuming sqr() is the square root function.

For example is the number 16, you are checking from 2 to sqr(16)=4. it divides by 2 and 4 but not 3. From 2 and 4, a bit of reverse maths would give you 8 (16/2) and 4 (16/4).

The number 27 would check from 2 to sqr(27)=5.19 (just up to 5 in this loop). You get:

* 2=no
* 3=yes (27/3 = 9), so 9=yes
* 4=no
* 5=no

You have all the factors: 1, 27, 3 and 9, though not necessarily in numerical order!

TomJS

2:42 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



yes i understand the br type i don't get why you have to say primeString=primeString? why do you have to have primeString in primeString
Also, i just realized its not all the prime before 1000 i need but the first 1000 primes, any ideas?

ajkimoto

7:21 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



TomJS,

I assume you are asking about:

primeString=(isPrime==1?primeString+'<br />'+i:primeString)

If so, this is just a shorthand for if-then-else, the syntax for which is:

var someValue = (condition)? trueValue : falseValue;

***

So, you need to get the first 1000 primes?

You will want to replace the outer for loop with a while loop, and increment a primeCount variable by one each time a prime is found.

With thanks to PCInk (and Isaac Newton) we have:

<head>
<script type="text/javascript">
<!--

//initialize primeCount to 0
var primeCount=0

//initialize empty string to hold prime numbers
var primeString=''

//initialize curPrime to 1
var curPrime=1

function checkPrime(){

//cycle through numbers until you have first 1000 primes
while(primeCount<=1000){

//initialize isPrime to 1 each time the while loop runs
varisPrime=1

//This calculates the square root of curPrime, used to create a more
//efficient prime check (as suggested by PCInk)
var X = 1;
for (var i = 0; i < 10; i++){
X = X - ((X*X - curPrime) / (2*X));
}

//cycle through factors
for (j=2; j<=X; j++){
//if divisible by factor, set isPrime to 0
if(curPrime%j==0){
isPrime=0
//exit loop
j=curPrime
}
}
//add a <br /> plus curPrime if curPrime is prime and increment primeCount
primeCount=(isPrime==1?primeCount+1:primeCount)
primeString=(isPrime==1?primeString+'<br />'+curPrime:primeString)
curPrime=curPrime+1
}

//after cycling through all numers, replace the contents of a div with
// id="primeDiv" with primeString
document.getElementById('primeDiv').innerHTML=primeString

}
//-->
</script>
</head>

<body onload="checkPrime()">
<h2>Here are the first 1000 prime numbers</h2>

<div id="primeDiv">Prime numbers will be displayed here shortly...</div>
</body>

Still a little slow, but much faster than my previous method.

ajkimoto

PCInk

7:35 pm on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The square root function is Math.sqrt(<num>)

ajkimoto

8:21 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



I THOUGHT that there ought to be a Math function to handle that!

ajkimoto

TomJS

9:42 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



OK thanks i get the while loop but you misunderstood my question. I understand it is an if type thing but i don't understand why when you define primeString you make it primeString=primeString+"<br>"+i instead of primeString="<br>"+i

ajkimoto

10:00 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



TomJS,

I set it up as

primeString=primeString+"<br />"+i

instead of

primeString="<br />"+i

because I wanted to continually add to primeString and then display it once I reached 1000 primes. If you set it up as

primeString="<br />"+i

then primeString would only contain a line break followed by the most recent prime found. This is a legitimate way to go (assuming that you append that string to the innerHTML of the div each time) but it takes more time to continually add text to the div than it does to write one long string just once.

BTW, the Math.sqrt() function is MUCH faster than my hand-built function (if you haven't tried it yet...)

ajkimoto

TomJS

10:03 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



yes i know I already knew about that function. Right now I am trying to get the whil loop to work. Here is the code I have so far. I changed a bunch of things to suit my style of code
var primeString=""
var primeCount=0
function checkPrime()
{
while(primeCount<=1000)
{

//initialize isPrime to 1 each time the i loop runs
var isPrime=1
//cycle through factors
for(i=1; i++)
{
}
for (j=2; j<=Math.sqrt(i); j++)
{
//if divisible by factor, set isPrime to 0
if(i%j==0)
{
isPrime=0
}
}
//add a <br /> plus i if i is prime
primeCount=(isPrime==1?primeCount+1:primeCount)
if (isPrime==1)
{
primeString=primeString+"<br>"+i
}
}
document.write(primeString)
}

TomJS

10:12 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



any idea what is wrong with it?

ajkimoto

10:11 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



TomJS,

This code works.

I initialized i to 1 outside the for loop, and increment it by one at the end of the for loop

ajkimoto

var primeString=""
var primeCount=0
function checkPrime(){

var i = 1

while(primeCount<=1000){

var isPrime=1

for (j=2; j<=Math.sqrt(i); j++)
{

if(i%j==0)
{
isPrime=0
}
}

primeCount=(isPrime==1?primeCount+1:primeCount)
if (isPrime==1){primeString=primeString+"<br>"+i}
i=i+1
}
document.write(primeString)
}

TomJS

3:33 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



Ah yeas thanks so much for your help!