Skip to content

Instantly share code, notes, and snippets.

@secondwtq
Created September 13, 2018 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secondwtq/8eb59a74fc4b3a7379fc58626184ea81 to your computer and use it in GitHub Desktop.
Save secondwtq/8eb59a74fc4b3a7379fc58626184ea81 to your computer and use it in GitHub Desktop.
open Printf
module IntSet = Set.Make(struct type t = int let compare = compare end)
let compute (base: int) (units: int list): IntSet.t =
printf "base: %d, (" base; List.iter (printf "%d, ") units; printf ")\n";
let rec f (digits: IntSet.t) (new_digits: IntSet.t) =
if IntSet.cardinal new_digits = 0
then digits
else begin
(* printf "("; IntSet.iter (printf "%d, ") new_digits; printf ") "; *)
let new_digits_now =
IntSet.fold (fun n set ->
(IntSet.fold (fun m set ->
let t = (n + m) mod base in
if IntSet.mem t digits then set else IntSet.add t set
) new_digits IntSet.empty) |> IntSet.union set
) digits IntSet.empty
in f (IntSet.union digits new_digits) new_digits_now
end
in
let digits = f (IntSet.singleton 0) (units |> IntSet.of_list) in
(* print_newline(); *)
printf "l: %d - " (IntSet.cardinal digits);
IntSet.iter (printf "%d, ") digits; print_newline (); print_newline ();
digits
let () =
compute 42 [6];
compute 42 [7];
compute 42 [6; 7];
let n = 29 in
let store: IntSet.t ref array array = Array.init (n + 1) (
fun _ -> Array.init (n + 1) (fun _ -> (ref IntSet.empty))) in
for i = 2 to n do
for j = 1 to (i - 1) do
store.(i).(j) := compute i [j]
done
done;
for i = 2 to n do
for j = 1 to (i - 1) do
for k = 1 to (i - 1) do
let t = compute i [j; k; ] in
let new_set = IntSet.union !(store.(i).(j)) !(store.(i).(k)) in
if IntSet.cardinal t != IntSet.cardinal new_set then
printf "===== !FAULT! =====\n";
done
done
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment