Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /** My Java solution to http://projecteuler.net/problem=13
  2.  *
  3.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  4.  * @version 29 SEPTEMBER 2012
  5.  *
  6.  * http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html
  7.  * http://www.leepoint.net/notes-java/data/numbers/10biginteger.html
  8.  *
  9.  * BigInteger is immutable. Therefore, you can't change sum, you need to reassign the result of the add method to sum.
  10.  * "input.dat"
  11.  */
  12. import java.math.BigInteger;//BigInteger superclass
  13. import java.io.*;//File
  14. import java.util.*;//Scanner
  15. public class solution13{
  16.     //set up inFile, SIZE and array
  17.     public static File            inFile   = new File("input.dat");//input in dat file
  18.     public static final int SIZE = 100;//size of input (100 number of 50 digits)
  19.     public static BigInteger [] array = new BigInteger[SIZE];//storing the input
  20.        
  21.     public static void main(String [] args)throws IOException {
  22.         //local declaration - stream and sum
  23.         Scanner sc = new Scanner(inFile);
  24.         BigInteger sum = new BigInteger("0");//sum of the BigInteger array
  25.         for (int i=0; i<SIZE; i++){
  26.             array[i] = new BigInteger(sc.nextLine());
  27.         }
  28.        
  29.         //testDisplay to test file input
  30.         //testDisplay();
  31.        
  32.         //Sum Them!
  33.         for (int i=0; i<SIZE; i++){
  34.             sum=sum.add( (array[i]) );
  35.         }
  36.         //Display sum
  37.         System.out.println("sum:");
  38.         System.out.println(sum.toString().substring(0,10));
  39.         //inStream.close();//close stream
  40.         sc.close();
  41.     }
  42.     public static void testDisplay(){
  43.         for (int i=0; i<SIZE; i++){
  44.             System.out.println(array[i]);
  45.         }
  46.     }
  47. }