Skip to content

Instantly share code, notes, and snippets.

@renatoalencar
Created April 17, 2021 22:41
Show Gist options
  • Save renatoalencar/364d396dd989deba8ac1d96de4849c00 to your computer and use it in GitHub Desktop.
Save renatoalencar/364d396dd989deba8ac1d96de4849c00 to your computer and use it in GitHub Desktop.
let valid_cpf cpf =
let aux size =
let sum = ref 0 in
for i = 0 to (size - 2) do
sum := !sum + cpf.(i) * (size - i);
done;
!sum * 10 mod 11
in
(aux 10) == cpf.(9) && (aux 11) == cpf.(10)
let join sep a =
Array.fold_left (fun acc value -> acc ^ sep ^ value) "" a
let print_array a =
a |> join " " |> print_endline;
exception Break
let next digits =
try
for i = 8 downto 0 do
if (digits.(i) < 9) then begin
digits.(i) <- digits.(i) + 1;
raise Break
end else begin
digits.(i) <- 0;
end
done
with Break -> ()
let should_stop digits =
Array.sub digits 0 9
|> Array.for_all (fun d -> d == 9)
let () =
let digits = [| 7; 4 |] in
let base = [| 0; 0; 0; 0; 0; 0; 0; 0; 0 |] in
let cpf = Array.append base digits in
let tries = ref 0 in
let count = ref 0 in
while not (should_stop cpf) do
cpf |> Array.map string_of_int |> print_array;
if valid_cpf cpf then
incr count;
incr tries;
next cpf;
done;
Printf.printf "Found %d, Tried: %d\n" !count !tries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment