woensdag 16 maart 2011

Euler problem 39

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximized?

analyse:


 1) a² + b² = c²
 2) a + b + c = p
 1+2) yield  
 3) b = (p²-2pa)/2(p-a)
 4) c = p - a - b

b is only integral when (p²-2pa) is evenly divisible by 2(p-a)

Performance improvements: 6 ms -> 0.2 ms

1) Don't count doubles {3,4,5} and {4,3,5}. alpha 0 - 45 deg. Maximum a = p/(2+√2)
 12 is the smallest perimeter which yield to an integral a, b & c {3,4,5}.
So answer must be a multiple of 12.



Problem39 =         840 elapsed time:    0 ms. Test Passed.

Geen opmerkingen:

Een reactie posten