Follow us on Twitter!
Capitalism is an Island of wealth in a sea of poverty
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 28
Guests Online: 25
Members Online: 3

Registered Members: 82905
Newest Member: BLckLIght
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Project Euler problem 7 - Javascript


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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
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?




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

RE: Project Euler problem 7 - Javascript


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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?


Author

RE: Project Euler problem 7 - Javascript

korg
Admin from hell



Posts: 2798
Location: ENDING YOUR ONLINE EXPERIENCE!
Joined: 01.01.06
Rank:
God
Posted on 23-03-10 07:23
Define is right on the money. Remove one of your closing braces and it will work fine.


i57.photobucket.com/albums/g215/korg1269/shodan13.jpg

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

RE: Project Euler problem 7 - Javascript


Member

Your avatar

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?

Code

<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>








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

RE: Project Euler problem 7 - Javascript


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
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 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. Smile

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.


Author

RE: Project Euler problem 7 - Javascript


Member

Your avatar

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

Here was the final code

Code
<script type="text/javascript">
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.


Author

RE: Project Euler problem 7 - Javascript


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 25-03-10 00:01
s3klyma wrote:
Thank you Define!
I got it after your suggestions Grin

No prob, man. Was interesting. Smile 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. Pfft You've obviously been in the forums before!