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 maximised?
Yeah I decided the easiest way to do this would be using a HashMap in Java for sum and occurrence pairs
I don't like coding in Java so I don't do it often, enjoy:
import java.util.*; class problem39 { public static void main (String [] args) { HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for(int i = 0; i <= 1000; i++) for(int j = 0; j <= (1000 - i); j++) for(int k = 0; k <= (1000 - (i+j)); k++) { if (i*i+j*j != k*k) continue; Integer sum = i+j+k; if(hm.containsKey(sum)) { Integer temp = hm.get(sum); temp = temp.intValue() + 1; hm.remove(sum); hm.put(sum, temp); } else hm.put(sum, 1); } Set<Map.Entry<Integer, Integer>> pairs = hm.entrySet(); Iterator<Map.Entry<Integer, Integer>> it = pairs.iterator(); int max = -1, result=-1; while(it.hasNext()) { Map.Entry<Integer, Integer> temp = it.next(); int val = temp.getValue().intValue(); if (val > max) { max = val; result = temp.getKey().intValue(); } } System.out.println(result); } }
Incidentally it was blazing fast, even with the JVM overhead and all that junk
$ time (java problem39) real 0m0.515s











