A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

Project Euler Problem 9

There are many formulas for generating Pythagorean triples. However, I’m going to use a brute force approach for this.

function getPythagoreanTriplet(abcSum::UInt64)
        exists::Bool = false;
        c::UInt64 = 0;
        for a::UInt64 in 1:1:abcSum-1
                for b::UInt64 in 1:1:abcSum-1
                        cSquared = a^2 + b^2
                        if floor(sqrt(cSquared)) == sqrt(cSquared)
                                c = UInt64(sqrt(cSquared))
                                if a+b+c == abcSum
                                        exists = true
                                        return a,b,c,exists
                                end
                        end
                end
        end
        return 0,0,0,exists
end

function parseARGS()
        if isempty(ARGS)
                abcSum::UInt64 = 1000;
        else
                abcSum = tryparse(UInt64,ARGS[1]);
        end
        return abcSum
end

function main()
        abcSum =  parseARGS();
        a,b,c,exists = getPythagoreanTriplet(abcSum);
        if exists
                println(a,"+",b,"+",c,"=",abcSum);
                println(a,"*",b,"*",c,"=",a*b*c);
        else
                println("There exists no solution");
        end
end

main()

$ julia ProjectEuler0009.jl 1000

200+375+425=1000
200*375*425=31875000
  0.000978 seconds (52 allocations: 1.906 KiB)


$ julia ProjectEuler0009.jl 25
There exists no solution
  0.000059 seconds (9 allocations: 576 bytes)


$ julia ProjectEuler0009.jl 10000
2000+3750+4250=10000
2000*3750*4250=31875000000
  0.087073 seconds (82 allocations: 2.375 KiB)