Problem:
The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.
There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0 [≤] x1, y1, x2, y2 [≤] 2.
Given that 0 [≤] x1, y1, x2, y2 [≤] 50, how many right triangles can be formed?
There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0 [≤] x1, y1, x2, y2 [≤] 2.
Given that 0 [≤] x1, y1, x2, y2 [≤] 50, how many right triangles can be formed?
Solution:
7652413
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{
public String run();
}
/*
* Solution to Project Euler problem 41
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p041 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p041().run());
}
public String run() {
for (int n = 9; n >= 1; n--) {
int[] digits = new int[n];
for (int i = 0; i < digits.length; i++)
digits[i] = i + 1;
int result = -1;
do {
if (Library.isPrime(toInteger(digits)))
result = toInteger(digits);
} while (Library.nextPermutation(digits));
if (result != -1)
return Integer.toString(result);
}
throw new RuntimeException("Not found");
}
private static int toInteger(int[] digits) {
int result = 0;
for (int x : digits)
result = result * 10 + x;
return result;
}
}
No comments :
Post a Comment