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.
23 lines
378 B
OCaml
23 lines
378 B
OCaml
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
|