Forum Moderators: open
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
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!
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
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
//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)
}
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)
}