Skip to content

Commit

Permalink
Merge pull request #33 from uninhm/crystal
Browse files Browse the repository at this point in the history
Implement in Crystal
  • Loading branch information
jabbalaci authored Jul 30, 2022
2 parents 29fd20e + 33d9ad9 commit eb85ec5
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 eb85ec5

Please sign in to comment.