Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Recursive powerset construction by doubling:
let rec gen xs = match xs with
| [] -> [[]]
| h :: rest ->
let sub = gen rest in
sub @ List.map (fun s -> h :: s) sub
Enumerates all 2^10 = 1024 subsets, filters by sum:
count = |{ S ⊆ {1..10} | Σ S = 15 }| = 20
Examples: {1,2,3,4,5}, {2,3,4,6}, {1,4,10}, {7,8}, {6,9}, ...
Tests recursive subset construction via List.map + closures,
pattern matching with h :: rest, List.fold_left (+) 0 sum reduce,
exhaustive O(2^n * n) traversal.
172 baseline programs total.
17 lines
322 B
OCaml
17 lines
322 B
OCaml
let rec gen xs =
|
|
match xs with
|
|
| [] -> [[]]
|
|
| h :: rest ->
|
|
let sub = gen rest in
|
|
sub @ List.map (fun s -> h :: s) sub
|
|
|
|
;;
|
|
|
|
let all = gen [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] in
|
|
let count = ref 0 in
|
|
List.iter (fun sub ->
|
|
let sum = List.fold_left (+) 0 sub in
|
|
if sum = 15 then count := !count + 1
|
|
) all;
|
|
!count
|