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:
