EULER
CÓDIGO EN JAVA
public class Euler {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
// precompute i^5 for i = 0..N
long[] five = new long[N+1];
for (int i = 0; i <= N; i++)
five[i] = (long) i * i * i * i * i;
System.out.println("Done precomputing fifth powers");
// now do exhaustive search
for (int e = 1; e <= N; e++) {
long e5 = five[e];
// restrict search to a <= b <= c <= d <= e
for (int a = 1; a <= N; a++) {
long a5 = five[a];
if (a5 + a5 + a5 + a5 > e5) break;
for (int b = a; b <= N; b++) {
long b5 = five[b];
if (a5 + b5 + b5 + b5 > e5) break;
for (int c = b; c <= N; c++) {
long c5 = five[c];
if (a5 + b5 + c5 + c5 > e5) break;
for (int d = c; d <= N; d++) {
long d5 = five[d];
if (a5 + b5 + c5 + d5 > e5) break;
if (a5 + b5 + c5 + d5 == e5)
System.out.println(a + "^5 + " + b + "^5 + "
+ c + "^5 + " + d + "^5 = " + e + "^5");
}
}
}
}
}
}
}
0 comentarios:
Publicar un comentario