Created
May 13, 2020 05:08
-
-
Save lukechampine/b45e8c5254756339dbe07aead96dc50c to your computer and use it in GitHub Desktop.
A FRACTRAN interpreter in Hoon
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
|= n=@ | |
=< | |
|^ | |
(fib-dec (interp fib-prog (fib-enc n))) | |
++ fib-prog | |
:~ 17^65 | |
133^34 | |
17^19 | |
23^17 | |
2.233^69 | |
23^29 | |
31^23 | |
74^341 | |
31^37 | |
41^31 | |
129^287 | |
41^43 | |
13^41 | |
1^13 | |
1^3 | |
== | |
++ fib-enc | |
|= n=@ | |
(mul 78 (pow 5 (dec n))) | |
++ fib-dec | |
|= n=@ | |
(dec (xeb n)) | |
-- | |
|% | |
:: | |
+$ frac [n=@ d=@] | |
:: | |
++ reduce-frac | |
|= x=frac | |
^- frac | |
=/ f d:(egcd x) | |
(div n.x f)^(div d.x f) | |
:: | |
++ mul-frac | |
|= [x=frac y=frac] | |
^- frac | |
%- reduce-frac | |
(mul n.x n.y)^(mul d.x d.y) | |
:: | |
++ first-div | |
|= [prog=(list frac) n=@] | |
^- (unit @) | |
?~ prog ~ | |
=/ m (mul-frac n^1 i.prog) | |
?: =(d.m 1) `n.m | |
$(prog t.prog) | |
:: | |
++ interp | |
|= [prog=(list frac) n=@] | |
^- @ | |
=/ d (first-div prog n) | |
?~ d n | |
$(n u.d) | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment