Problem:
The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.
Solution:
171
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{
public String run();
}
/*
* Solution to Project Euler problem 19
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p019 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p019().run());
}
public String run() {
int count = 0;
for (int y = 1901; y <= 2000; y++) {
for (int m = 1; m <= 12; m++) {
if (dayOfWeek(y, m, 1) == 0) // Sunday
count++;
}
}
return Integer.toString(count);
}
private static int dayOfWeek(int year, int month, int day) {
long m = mod((long)month - 3, 4800);
long y = mod(year + m / 12, 400);
m %= 12;
return (int)((y + y/4 - y/100 + (13 * m + 2) / 5 + day + 2) % 7);
}
private static long mod(long x, long y) {
x %= y;
if (y > 0 && x < 0 || y < 0 && x > 0)
x += y;
return x;
}
}
No comments :
Post a Comment