Skip to content

Commit

Permalink
Implement in Crystal
Browse files Browse the repository at this point in the history
  • Loading branch information
uninhm committed Jul 29, 2022
1 parent 2d96dfc commit 33d9ad9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crystal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
crystal build --release main.cr
23 changes: 23 additions & 0 deletions crystal/main.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MAX_N = 440_000_000

def is_munchausen(cache, num)
n = num
total = 0
while n > 0
digit = n % 10
total += cache[digit]
if total > num
return false
end
n //= 10
end
return total == num
end

cache = [0] + (1..9).map { |n| n**n }

(0 .. MAX_N).each do |i|
if is_munchausen(cache, i)
puts i
end
end

0 comments on commit 33d9ad9

Please sign in to comment.