Join us at IRC!
Capitalism is an Island of wealth in a sea of poverty
Friday, May 25, 2012
Navigation
Members Online
Total Online: 29
Web Spiders: 16
Guests Online: 29
Members Online: 0

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Project Euler problem 7 - Javascript

s3klyma
Member

Posts: 153
Location:
Joined: 02.12.07
Rank:
HBH Guru
Posted on 23-03-10 02:14
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

<scblockedript type="text/javascblockedript">
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);
}
}
}
}
}
</scblockedript>


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?




"Shut the fuck up and quit being a naive fucktard." - Zephyr_Pure

"Oh is that so? Well fuck my porridge, you've convinced me,
everyone's just really mean to you." - COM



Edited by s3klyma on 23-03-10 02:15
Author

RE: Project Euler problem 7 - Javascript

define
Member

Posts: 201
Location:
Joined: 13.12.08
Rank:
Moderate
Warn Level: 1
Posted on 23-03-10 03:08
My count might be off here, but I see 4 opening braces and 5 closing ones.

Error Console?


If you need to contact me, send me a PM. I will read and/or respond in time.
Author

RE: Project Euler problem 7 - Javascript

korg
Admin from hell



Posts: 1704
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06
Rank:
The Master
Posted on 23-03-10 07:23
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

s3klyma
Member

Posts: 153
Location:
Joined: 02.12.07
Rank:
HBH Guru
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 scblockedript.

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?


<scblockedript type="text/javascblockedript">
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);
</scblockedript>





"Shut the fuck up and quit being a naive fucktard." - Zephyr_Pure

"Oh is that so? Well fuck my porridge, you've convinced me,
everyone's just really mean to you." - COM



Edited by s3klyma on 24-03-10 03:37
Author

RE: Project Euler problem 7 - Javascript

define
Member

Posts: 201
Location:
Joined: 13.12.08
Rank:
Moderate
Warn Level: 1
Posted on 24-03-10 03:52
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 Javascblockedript could handle the type of involved recursion that you're performing. Javascblockedript 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 Javascblockedript.


If you need to contact me, send me a PM. I will read and/or respond in time.
Author

RE: Project Euler problem 7 - Javascript

s3klyma
Member

Posts: 153
Location:
Joined: 02.12.07
Rank:
HBH Guru
Posted on 24-03-10 04:08
Thank you Define!
I got it after your suggestions :D

Here was the final code

<scblockedript type="text/javascblockedript">
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;
}
}
}

</scblockedript>


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.




"Shut the fuck up and quit being a naive fucktard." - Zephyr_Pure

"Oh is that so? Well fuck my porridge, you've convinced me,
everyone's just really mean to you." - COM

Author

RE: Project Euler problem 7 - Javascript

define
Member

Posts: 201
Location:
Joined: 13.12.08
Rank:
Moderate
Warn Level: 1
Posted on 25-03-10 00:01
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!


If you need to contact me, send me a PM. I will read and/or respond in time.
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.