Skip to content

Commit cd22238

Browse files
committed
feat(tests, rosetta): adding Munchhausen example
1 parent dfb6038 commit cd22238

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(import std.List)
2+
3+
(let self-exponent (fun (x n acc)
4+
(if (> n 0)
5+
(self-exponent x (- n 1) (* x acc))
6+
acc)))
7+
8+
(let cache (list:map (list:iota 0 10) (fun (x) (if (= x 0) 0 (self-exponent x x 1)))))
9+
10+
(let is_munchhausen (fun (number) {
11+
(mut total 0)
12+
(mut n number)
13+
(mut continue true)
14+
15+
(while (and (> n 0) continue) {
16+
(let digit (mod n 10))
17+
(set total (+ total (@ cache digit)))
18+
(if (> total number)
19+
(set continue false)
20+
(set n (math:floor (/ n 10)))) })
21+
22+
(= total number) }))
23+
24+
25+
(assert (is_munchhausen 1) "1 is a Munchhausen number")
26+
(assert (is_munchhausen 3435) "3435 is a Munchhausen number")
27+
28+
(assert (not (is_munchhausen 677)) "677 is not a Munchhausen number")

0 commit comments

Comments
 (0)