Project Euler > Problem 75 > Singular integer right triangles (Java Solution)

Problem:

It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.

12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)

In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.

120 cm: (30,40,50), (20,48,52), (24,45,51)

Given that L is the length of the wire, for how many values of L [≤] 1,500,000 can exactly one integer sided right angle triangle be formed?


Solution:

4782

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

public interface EulerSolution{

public String run();

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

import java.math.BigInteger;


public final class p025 implements EulerSolution {

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


private static final int DIGITS = 1000;

public String run() {
BigInteger lowerthres = BigInteger.TEN.pow(DIGITS - 1);
BigInteger upperthres = BigInteger.TEN.pow(DIGITS);
BigInteger prev = BigInteger.ONE;
BigInteger cur = BigInteger.ZERO;
int i = 0;
while (true) {
// At this point, prev = fibonacci(i - 1) and cur = fibonacci(i)
if (cur.compareTo(lowerthres) >= 0)
return Integer.toString(i);
else if (cur.compareTo(upperthres) >= 0)
throw new RuntimeException("Not found");

BigInteger temp = cur.add(prev);
prev = cur;
cur = temp;
i++;
}
}

}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact