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

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:
2026-05-10 03:02:17 +00:00
parent 5c1b4349aa
commit 36e02c906a
3 changed files with 26 additions and 0 deletions

View File

@@ -105,6 +105,7 @@
"poly_stack.ml": 5,
"pow_mod.ml": 738639,
"prime_factors.ml": 17,
"pythagorean.ml": 16,
"queens.ml": 2,
"quicksort.ml": 44,
"roman.ml": 44,

View 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