ocaml: phase 5.1 pythagorean.ml baseline (primitive Pythagorean triples with hyp <= 100 = 16)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Uses Euclid's formula: for coprime m > k of opposite parity, the
triple (m^2 - k^2, 2mk, m^2 + k^2) is a primitive Pythagorean.
let count_primitive_triples n =
let c = ref 0 in
for m = 2 to 50 do
let kk = ref 1 in
while !kk < m do
if (m - !kk) mod 2 = 1 && gcd m !kk = 1 then begin
let h = m * m + !kk * !kk in
if h <= n then c := !c + 1
end;
kk := !kk + 1
done
done;
!c
count_primitive_triples 100 = 16
The 16 triples include the classics (3,4,5), (5,12,13), (8,15,17),
(7,24,25), and end with (65,72,97).
134 baseline programs total.
This commit is contained in:
19
lib/ocaml/baseline/pythagorean.ml
Normal file
19
lib/ocaml/baseline/pythagorean.ml
Normal file
@@ -0,0 +1,19 @@
|
||||
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
|
||||
|
||||
let count_primitive_triples n =
|
||||
let c = ref 0 in
|
||||
for m = 2 to 50 do
|
||||
let kk = ref 1 in
|
||||
while !kk < m do
|
||||
if (m - !kk) mod 2 = 1 && gcd m !kk = 1 then begin
|
||||
let h = m * m + !kk * !kk in
|
||||
if h <= n then c := !c + 1
|
||||
end;
|
||||
kk := !kk + 1
|
||||
done
|
||||
done;
|
||||
!c
|
||||
|
||||
;;
|
||||
|
||||
count_primitive_triples 100
|
||||
Reference in New Issue
Block a user