Project Euler > Problem 120 > Square remainders (Java Solution)

Problem:

Let r be the remainder when (a[−]1)n + (a+1)n is divided by a2.

For example, if a = 7 and n = 3, then r = 42: 63 + 83 = 728 [≡] 42 mod 49. And as n varies, so too will r, but for a = 7 it turns out that rmax = 42.

For 3 [≤] a [≤] 1000, find [∑] rmax.


Solution:

648

Code:
The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}
/* 
* Solution to Project Euler problem 20
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/


public final class p020 implements EulerSolution {

public static void main(String[] args) {
System.out.println(new p020().run());
}


public String run() {
String temp = Library.factorial(100).toString();
int sum = 0;
for (int i = 0; i < temp.length(); i++)
sum += temp.charAt(i) - '0';
return Integer.toString(sum);
}

}

No comments:

Post a Comment