Ian Dorian Macleod
Problem 39
Problem Statement

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 $\leq$ 1000, is the number of solutions maximised?

url: https://projecteuler.net/problem=39
Approach

C++ implementation

#include <stdc++.h>

using namespace std;

// brute force approach, not particularly graceful 

int maxNumTri(int n) {
    unordered_set st;
    int i = 1;
    int j = 1;
    for(i = 1; i <= n/4; i++) { 
        for(j = i+1; j <= (n-i)/2; j++) { 
            float tmp = sqrt(pow(i,2) + pow(j,2));
            if ((int)(tmp * 10) % 10 == 0 && tmp + i + j == n) {
                st.insert(i);
            }
        }
    }
    return st.size();
}

int main() {
    int maxindex = 0;
    int max = 0;
    for(int i = 4; i <= 1000; i++) {
        int tmp = maxNumTri(i);
        if (tmp > max) {
            maxindex = i;
            max = tmp;
        }
        cout << i << endl;
    }
    cout << maxindex << endl;
    return 0;
}


You can also download the source code for this problem here and compile it on your local machine.
Further Analysis