ocaml: phase 5.1 euler9.ml baseline (Project Euler #9, abc = 31875000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
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.
This commit is contained in:
17
lib/ocaml/baseline/euler9.ml
Normal file
17
lib/ocaml/baseline/euler9.ml
Normal file
@@ -0,0 +1,17 @@
|
||||
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 ()
|
||||
@@ -27,6 +27,7 @@
|
||||
"euler1.ml": 233168,
|
||||
"euler2.ml": 4613732,
|
||||
"euler6.ml": 25164150,
|
||||
"euler9.ml": 31875000,
|
||||
"expr_eval.ml": 16,
|
||||
"expr_simp.ml": 22,
|
||||
"factorial.ml": 3628800,
|
||||
|
||||
Reference in New Issue
Block a user