ocaml: phase 5.1 euler29_small.ml baseline (distinct a^b for 2<=a,b<=5 = 15)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s

Compute every power a^b for a, b in [2..N] and count distinct
values. Hashtbl as a set with unit-payload (iter-168 idiom):

  let euler29 n =
    let h = Hashtbl.create 64 in
    for a = 2 to n do
      for b = 2 to n do
        let p = ref 1 in
        for _ = 1 to b do p := !p * a done;
        Hashtbl.replace h !p ()
      done
    done;
    Hashtbl.length h

For N=5: 16 powers minus one duplicate (4^2 = 2^4 = 16) -> 15.

Real PE29 uses N=100 (answer 9183).

120 baseline programs total — milestone.
This commit is contained in:
2026-05-09 23:37:36 +00:00
parent 87f9a84365
commit 21dbd195d5
3 changed files with 20 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
let euler29 n =
let h = Hashtbl.create 64 in
for a = 2 to n do
for b = 2 to n do
let p = ref 1 in
for _ = 1 to b do p := !p * a done;
Hashtbl.replace h !p ()
done
done;
Hashtbl.length h
;;
euler29 5

View File

@@ -33,6 +33,7 @@
"euler21_small.ml": 504,
"euler25.ml": 55,
"euler28.ml": 261,
"euler29_small.ml": 15,
"euler30_cube.ml": 1301,
"euler3.ml": 29,
"euler4_small.ml": 9009,

View File

@@ -407,6 +407,11 @@ _Newest first._
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
'a tree`) with insert + in-order traversal. Tests parametric ADT,
recursive match, List.append, List.fold_left.
- 2026-05-09 Phase 5.1 — euler29_small.ml baseline (distinct a^b for
2≤a,b≤5 = 15). 16 powers minus the one duplicate (4^2 = 2^4 = 16)
→ 15 distinct values. Hashtbl as a set with unit-payload (the
iter-168 idiom). Real PE29 uses N=100 (answer 9183). 120 baseline
programs total — milestone.
- 2026-05-09 Phase 5.1 — euler21_small.ml baseline (sum of amicable
numbers ≤ 300 = 504). div_sum computes proper divisor sum via
trial division up to √n; outer loop finds amicable pairs (a, b)