forked from jabbalaci/SpeedTests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.exs
44 lines (35 loc) · 843 Bytes
/
main.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env elixir
defmodule Munchausen do
# @cache [0] ++ for n <- 1..9, do: n ** n
@cache for i <- 1..9, into: %{0 => 0}, do: {i, i ** i}
def get_cache(), do: @cache
def explode(n), do: explode(n, [])
# int, acc -> list[int]
def explode(n, acc) when n == 0, do: acc
def explode(n, acc) do
digit = rem(n, 10)
explode(div(n, 10), [digit | acc])
end
# int -> bool
def is_munchausen(n) do
digits = explode(n)
li = for x <- digits, do: @cache[x]
n == Enum.sum(li)
end
end
defmodule Main do
# @max 10_000
@max 440_000_000
def main() do
# Munchausen.get_cache() |> IO.inspect
Enum.each(0..@max, fn n ->
# if rem(n, 1_000_000) == 0 do
# IO.puts("# #{n}")
# end
if Munchausen.is_munchausen(n) do
IO.puts(n)
end
end)
end
end
Main.main()