ocaml: phase 5.1 prime_factors.ml baseline (trial-division, 360 factor sum = 17)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Three refs threading through a while loop:
m remaining quotient
d current divisor
result accumulator (built in reverse, List.rev at end)
while !m > 1 do
if !m mod !d = 0 then begin
result := !d :: !result;
m := !m / !d
end else
d := !d + 1
done
360 = 2^3 * 3^2 * 5 factors to [2;2;2;3;3;5], sum 17.
60 baseline programs total — milestone.
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
"pi_leibniz.ml": 314,
|
||||
"pretty_table.ml": 64,
|
||||
"poly_stack.ml": 5,
|
||||
"prime_factors.ml": 17,
|
||||
"queens.ml": 2,
|
||||
"quicksort.ml": 44,
|
||||
"roman.ml": 44,
|
||||
|
||||
16
lib/ocaml/baseline/prime_factors.ml
Normal file
16
lib/ocaml/baseline/prime_factors.ml
Normal file
@@ -0,0 +1,16 @@
|
||||
let factor n =
|
||||
let result = ref [] in
|
||||
let m = ref n in
|
||||
let d = ref 2 in
|
||||
while !m > 1 do
|
||||
if !m mod !d = 0 then begin
|
||||
result := !d :: !result;
|
||||
m := !m / !d
|
||||
end else
|
||||
d := !d + 1
|
||||
done;
|
||||
List.rev !result
|
||||
|
||||
;;
|
||||
|
||||
List.fold_left (+) 0 (factor 360)
|
||||
@@ -407,6 +407,12 @@ _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 — prime_factors.ml baseline (trial-division
|
||||
factorisation, sum of factors of 360 = 17). Three refs threading
|
||||
through a while loop: m holds the remaining quotient, d the
|
||||
current divisor, result accumulates factors. When `m mod d = 0`,
|
||||
push d and divide; otherwise increment d. 360 = 2^3 * 3^2 * 5
|
||||
factors to [2;2;2;3;3;5], sum 17. 60 baseline programs total.
|
||||
- 2026-05-09 Phase 5.1 — atm.ml baseline (mutable record + custom
|
||||
exception + try/with, balance 120 after rollback). Models a bank
|
||||
account: deposit/withdraw mutate the balance field; an over-draw
|
||||
|
||||
Reference in New Issue
Block a user