Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Find the unique Pythagorean triple with a + b + c = 1000 and
return their product.
The naive triple loop timed out under host contention (10-minute
cap exceeded with ~333 * 999 ~= 333k inner iterations of complex
checks). Rewritten with algebraic reduction:
a + b + c = 1000 AND a^2 + b^2 = c^2
=> b = (500000 - 1000 * a) / (1000 - a)
so only the outer a-loop is needed (333 iterations). Single-pass
form:
for a = 1 to 333 do
let num = 500000 - 1000 * a in
let den = 1000 - a in
if num mod den = 0 then begin
let b = num / den in
if b > a then
let c = 1000 - a - b in
if c > b then result := a * b * c
end
done
Triple (200, 375, 425), product 31875000.
103 baseline programs total.
18 lines
319 B
OCaml
18 lines
319 B
OCaml
let euler9 () =
|
|
let result = ref 0 in
|
|
for a = 1 to 333 do
|
|
let num = 500000 - 1000 * a in
|
|
let den = 1000 - a in
|
|
if num mod den = 0 then begin
|
|
let b = num / den in
|
|
if b > a then
|
|
let c = 1000 - a - b in
|
|
if c > b then result := a * b * c
|
|
end
|
|
done;
|
|
!result
|
|
|
|
;;
|
|
|
|
euler9 ()
|