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.