Project Euler – Problem # 13 – Solved with Java

Find the first ten digits of the sum of one-hundred 50-digit numbers.

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

One Possible Solution: Java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;


public class Problem_13 {
	public static void main(String [] args) {
		try
	    {
	       FileReader fro = new FileReader( "one-hundred_50.txt" );
	       BufferedReader bro = new BufferedReader( fro );
	  
	       BigInteger tally = BigInteger.ZERO;
	     
	       String line;
	       while ((line = bro.readLine()) != null)   {
	           BigInteger bi = new BigInteger(line);
	           tally = tally.add(bi);
	       }
	       System.out.println("tally = " + tally);
	    }
		catch( FileNotFoundException filenotfoundexcption )
		   {
		     System.out.println( "one-hundred_50.txt, does not exist" );
		   }
		 
		catch( IOException ioexception )
		   {
		     ioexception.printStackTrace( );
		   }
	}

}
Advertisements

Project Euler – Problem # 13 – Solved with Go

Find the first ten digits of the sum of one-hundred 50-digit numbers.

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

**********

I had to ask two questions on stackoverflow.com to help me solve this problem in Go. Question # 1 was about reading in the external file and getting it into a useable form (string). Question # 2 was about the “math/big” package. For numbers greater than 2147483647, the big package is needed.

One Possible Solution: Go

package main

import "fmt"
import "io/ioutil"
import "strings"
import "math/big"

func main() {
	fData, err := ioutil.ReadFile("one-hundred_50.txt")	// read in the external file
	if err != nil {
		fmt.Println("Err is ", err) 	// print any error
	}
	strbuffer := string(fData)	// convert read in file to a string
	lines := strings.Split(strbuffer, "\n") // split the string into lines
	tally := big.NewInt(0)		// declare and initialise big number tally
	for _, line := range lines {	// loop through each line in lines - 100 of them
		bi := big.NewInt(0)	// declare and  initialise big number bi
		bi.SetString(line, 10) 	// convert line to a big integer (base 10)
		tally.Add(tally, bi)   	// add bi to the tally
	}
	fmt.Println(tally) 	// print tally and use the first 10 digits for the answer
}

Project Euler – Problem # 13 – Solved with Python

Python can handle these large number (50 digits and their addition) without any special handling

Problem:

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

# Python version = 2.7.2
# Platform = win32

def main():
    f = open('one-hundred_50.txt', 'rU')
    tally = 0 
    for g in f.readlines():
        gpl = g.replace('\n', '')
        if gpl.isdigit():
            number = int(gpl)
            tally = number + tally
        else:
            print "The varialble gpl contains no digit"
    f.close()
    print "tally = ", tally

if __name__ == '__main__':
    main()

Project Euler