ocaml: phase 5.1 euler30_cube.ml baseline (sum of digit-cube narcissistic numbers <= 999 = 1301)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

Numbers equal to the sum of cubes of their digits:
  153 = 1 + 125 + 27
  370 = 27 + 343 + 0
  371 = 27 + 343 + 1
  407 = 64 + 0 + 343
  sum = 1301

Implementation:
  pow_digit_sum n p   walk digits of n, accumulate d^p
  euler30 p limit     scan 2..limit and sum where pow_digit_sum n p = n

Real PE30 uses 5th powers (answer 443839); the cube version
exercises the same algorithm in a smaller search space.

118 baseline programs total.
This commit is contained in:
2026-05-09 23:17:00 +00:00
parent ea7120751d
commit 46e49dc947
3 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
let pow_digit_sum n p =
let m = ref n in
let s = ref 0 in
while !m > 0 do
let d = !m mod 10 in
let pd = ref 1 in
for _ = 1 to p do pd := !pd * d done;
s := !s + !pd;
m := !m / 10
done;
!s
let euler30 p limit =
let total = ref 0 in
for n = 2 to limit do
if pow_digit_sum n p = n then total := !total + n
done;
!total
;;
euler30 3 999

View File

@@ -32,6 +32,7 @@
"euler2.ml": 4613732,
"euler25.ml": 55,
"euler28.ml": 261,
"euler30_cube.ml": 1301,
"euler3.ml": 29,
"euler4_small.ml": 9009,
"euler5.ml": 232792560,

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 — euler30_cube.ml baseline (sum of numbers
equal to sum of cubes of their digits, ≤999 = 1301). The full
numbers are 153 + 370 + 371 + 407 = 1301. PE30 proper uses 5th
powers (answer 443839); cube version exercises the same algorithm
in a smaller search space. 118 baseline programs total.
- 2026-05-09 Phase 5.1 — euler28.ml baseline (sum of diagonals in
7x7 number spiral = 261). For each layer (1..(n-1)/2) the four
corners are spaced 2*layer apart, so we step `k` four times per