• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Project Euler problem 10

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to solve problem 10 of Project Euler .I wrote a code ,but it does not work for higher values.


Can anyone please help me out???
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll probably overflow the long. Try it with BigInteger.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "higher values" - higher than 2000000? I think even an "int" should work fine for all involved fields for the original problem.

If you start with "boolean b = true", then you don't need the "if (i == num)" block. One should avoid accessing loop variables outside of the "for" block.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rajarshi roy wrote:I wrote a code ,but it does not work for higher values.


In what way does it not work? Do you get the wrong answer? Does it throw an exception? Does it take an unacceptably long time to finish?

Looking at your code, I suspect it's the latter. It's going to take a long time to check all those values to see if they're prime. To find is a value n is prime, do you really have to loop through every number less than n? There are a couple of huge improvements you can make on this method that will make it much faster.

Wouter Oet wrote:You'll probably overflow the long. Try it with BigInteger.


I'm pretty sure this problem can be done without BigInteger. All the primes are less than 2 million, and there are much less than 2 million of them, so their sum should be much less than 2 million times 2 million, which is 4 trillion. That should fit easily inside a long.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look up something called the Sieve of Eratosthenes. It is a really quick way to test if a number is prime.

-Hunter
 
rajarshi roy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code works fine for smaller values,say below 100000.But it takes exceptionally long time to finish for values like 1000000.I will try to change the code.Thanks for your advices.
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic