Sunday, September 30, 2012

Project Euler Problem 13 (continued)

Alhamdulillah, got it. It is kind of cheating of me, because I used Java this time. Using BigInteger, Scanner and File classes, that was almost easy. I did stumbled a bit, but a lot easier than making C++ one.

I used Scanner & File for file input. Then, utilizing the simple to use BigInteger to create array that will store input. BigInteger is another class, so I required a special method add() to add the BigInteger later. Sum of type BigInteger to be sum counter. But, BigInteger is immutable, so I have to re-initalize it.

sum = sum.add(array[i]);

Then display only the first 10 digits. Change first to string with ever-popular toString()substring(0,10) is used that way so it will start from the beginning until index (10-1), or in other word, first 10 characters.

 
"input.dat"

No comments:

Post a Comment