Project Euler problem 7 - Javascript
ProjectEuler wrote: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
Here is my code
<script type="text/javascript">
var d = 0;
for(i=0;i<=999999999;i++){
for(a=i;a>=0;a--){ //Is it prime?
if(i%a!=0){
document.write(i+"<br>");
d++;
if(d==10001){ //Alert 10,001st
alert(i);
}
}
}
}
}
</script>
It's not the most optimized, but it should get the job done. It doesn't print anything out, and doesn't alert anything. What am I doing wrong?
Thanks. Now it works. But I also figured out that it's faulty.
It doesn't just tell me if the number is prime, it tells me every time a number lower than 'i' doesn't go evenly into 'i'.
So here is another idea: If I set a boolean variable, prime, to true. Then during the second for loop, I can check if i%a == 0, and if it is, I'll set prime to false. So at the end of the loop, if prime is still true, then I'll know that it's a prime number.
I'll post back when I have a working script.
Edit: Also, in the second for loop, I wrote for(a=i;a>=0;a–) But then it would also check if i%i == 0, and if i%1 == 0, and they both would, so it would always report back non-prime.
Editedit:
This is my current code, and I've triple checked the number of braces, but still it just freezes firefox. What did I do wrong?
<script type="text/javascript">
var d = 0;
var prime = true;
while( d < 10002 ){
for(i=3;i<=9999999;i++){
prime = true;
for(a=(i-1);a>=2;a--){
if(i%a==0){
prime = false;
}
}
if( prime == true ){
d++;
document.write(i+"<br>");
}
}
}
alert(d);
</script>
s3klyma wrote: So here is another idea: If I set a boolean variable, prime, to true. Then during the second for loop, I can check if i%a == 0, and if it is, I'll set prime to false.
As long as a is not equal to i and a is not equal to 1, then that probably could work.
You could also probably get far by checking a in the range of 1 to 20, as well as verifying the two conditions above. That should cover a large quantity of prime matches, though you'll have to adjust the range as needed.
s3klyma wrote: This is my current code, and I've triple checked the number of braces, but still it just freezes firefox. What did I do wrong?
You assumed that Javascript could handle the type of involved recursion that you're performing. Javascript runs in the memory space of the browser so, when you require a lot of it, it locks it up for a great deal of time. :)
Try reducing your primary loop to something much lower than 999999, and try the 20 range suggestion I have above. Adjust higher as needed to obtain the desired result.
Also, if you have any choice at all, I'd suggest against using Javascript.
Thank you Define! I got it after your suggestions :D
Here was the final code
var d = 0;
var prime = true;
for(i=2;i<=200000;i++){
prime = true;
for(a=(i-1);a>=2;a--){
if(i%a==0){
prime = false;
}
}
if( prime == true ){
d++;
if(d == 10001){
alert(i);
break;
}
}
}
</script>
And I will likely go back to using PHP for the challenges, but I got my php hosting account banned and haven't yet had the chance to install it locally.
s3klyma wrote: Thank you Define! I got it after your suggestions :D
No prob, man. Was interesting. :) Thanks for posting the final code, too.
s3klyma wrote: And I will likely go back to using PHP for the challenges, but I got my php hosting account banned and haven't yet had the chance to install it locally. Very precise answer. :P You've obviously been in the forums before!