Created
September 26, 2020 20:10
-
-
Save Anderssorby/bd0e314d1ccdc11a8c876c5521af122d to your computer and use it in GitHub Desktop.
Play with the Collatz conjecture in Formality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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