Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** My Java solution to http://projecteuler.net/problem=13
- *
- * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
- * @version 29 SEPTEMBER 2012
- *
- * http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html
- * http://www.leepoint.net/notes-java/data/numbers/10biginteger.html
- *
- * BigInteger is immutable. Therefore, you can't change sum, you need to reassign the result of the add method to sum.
- * "input.dat"
- */
- import java.math.BigInteger;//BigInteger superclass
- import java.io.*;//File
- import java.util.*;//Scanner
- public class solution13{
- //set up inFile, SIZE and array
- public static File inFile = new File("input.dat");//input in dat file
- public static final int SIZE = 100;//size of input (100 number of 50 digits)
- public static BigInteger [] array = new BigInteger[SIZE];//storing the input
- public static void main(String [] args)throws IOException {
- //local declaration - stream and sum
- Scanner sc = new Scanner(inFile);
- BigInteger sum = new BigInteger("0");//sum of the BigInteger array
- for (int i=0; i<SIZE; i++){
- array[i] = new BigInteger(sc.nextLine());
- }
- //testDisplay to test file input
- //testDisplay();
- //Sum Them!
- for (int i=0; i<SIZE; i++){
- sum=sum.add( (array[i]) );
- }
- //Display sum
- System.out.println("sum:");
- System.out.println(sum.toString().substring(0,10));
- //inStream.close();//close stream
- sc.close();
- }
- public static void testDisplay(){
- for (int i=0; i<SIZE; i++){
- System.out.println(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement