RE: Project Euler problem 7 - Javascript

Define is right on the money. Remove one of your closing braces and it will work fine.

I deal in pain, All life I drain, I dominate, I seal your fate.

Author

RE: Project Euler problem 7 - Javascript

Member

Posts:Location:Joined: 01.01.70 Rank: Guest

Posted on 24-03-10 03:13

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?

RE: Project Euler problem 7 - Javascript

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.