Euler Problem 20

n! means n × (n − 1) ×× 3 × 2 × 1

Find the sum of the digits in the number 100!

Easy again if you use the BigInteger class.

import java.math.BigInteger;

public class Euler20
{
    public static void main(String[] args)
    {
        System.out.print(“Problem 20:\n”);
        Euler20 e = new Euler20();
        System.out.print(“Answer = ” + e.Problem()+ “\n”);
    }
    public String Problem ()
    {
        long sum = 0;
        BigInteger omg = new BigInteger(“1”);
        for (int i=1; i <=100; i++)
            omg = omg.multiply(new BigInteger(String.valueOf(i)));
        String list = omg.toString();
        for (int i=0; i <= list.length()-1; i++)
            sum += Integer.parseInt(String.valueOf(list.charAt(i)));
        return String.valueOf(sum);
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s