diff --git a/crystal/Makefile b/crystal/Makefile new file mode 100644 index 0000000..2e54eef --- /dev/null +++ b/crystal/Makefile @@ -0,0 +1,2 @@ +build: + crystal build --release main.cr diff --git a/crystal/main.cr b/crystal/main.cr new file mode 100644 index 0000000..b609133 --- /dev/null +++ b/crystal/main.cr @@ -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