Skip to content

Instantly share code, notes, and snippets.

@Anderssorby
Created September 26, 2020 20:10
Show Gist options
  • Save Anderssorby/bd0e314d1ccdc11a8c876c5521af122d to your computer and use it in GitHub Desktop.
Save Anderssorby/bd0e314d1ccdc11a8c876c5521af122d to your computer and use it in GitHub Desktop.
Play with the Collatz conjecture in Formality
Collatz.run: IO(Unit)
do IO {
var val_str = IO.prompt("start value:");
let start = Nat.parse_decimal(val_str);
Collatz.step(start);
}
Collatz.step(start: Nat): IO(Unit)
let next = Collatz.next(start);
do IO {
IO.print(Nat.show(next));
case Nat.eql(next, 1):
| Bool.true => IO.print("Loop found!");
| Bool.false => Collatz.step(next);;
}
Collatz.next(start: Nat): Nat
if Nat.odd(start) then
Nat.add(Nat.mul(3, start), 1)
else
Nat.div(start, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment