Project Euler : Digits Of Sum of 1^1 + 2^2 + 3 ^ 3 …

Nothing to talk about here. Simple implementation.

Main class:

package problems_40_50;

public class Problem_48 {
	private static final long MAX_LENGTH = 10000000000L;

	/**
	 * Multiplies two long numbers and truncates the sum length to the MAX_LENGTH
	 *
	 * @return
	 */
	private long multiply(long a, long b) {
		return a * b % Problem_48.MAX_LENGTH;
	}

	public long sumOfSeries(int maxNumber) {
		long[] numberGrid = new long[maxNumber];
		for (int i = 0; i < maxNumber; i++)
			numberGrid[i] = i + 1;

		for (int i = 1; i < maxNumber; i++)
			for (int j = i; j < maxNumber; j++) {
				if ((j + 1) % 10 == 0)
					continue;
				numberGrid[j] = multiply(numberGrid[j], j + 1);
			}

		long sum = 0;
		for (int i = 0; i < maxNumber; i++) {
			if ((i + 1) % 10 == 0)
				continue;
			sum += numberGrid[i];
		}

		return sum;
	}

	public static void main(String[] args) {
		Problem_48 p = new Problem_48();
		System.out.println(p.sumOfSeries(1000) % Problem_48.MAX_LENGTH);
	}
}

Popularity: 4% [?]

Related posts:

  1. Project Euler : Largest Sum Of Digits In Exponentiation
  2. Project Euler : Numbers That Are Fifth Powers Of Their Digits
  3. Project Euler : Greatest Product of 4 Numbers In A 20×20 Grid
  4. Project Euler : Fast Factorial Computation
  5. Project Euler : Sum Of Digits In a^b