Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
25
Task/Digital-root/Elixir/digital-root.elixir
Normal file
25
Task/Digital-root/Elixir/digital-root.elixir
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
defmodule Digital do
|
||||
def root(n, base \\ 10), do: root(n, base, 0)
|
||||
|
||||
def root(n, base, ap) when n < base, do: {n, ap}
|
||||
def root(n, base, ap) do
|
||||
Integer.to_string(n, base)
|
||||
|> String.codepoints
|
||||
|> Enum.reduce(0, fn x,acc -> acc + String.to_integer(x, base) end)
|
||||
|> root(base, ap+1)
|
||||
end
|
||||
end
|
||||
|
||||
data = [627615, 39390, 588225, 393900588225]
|
||||
Enum.each(data, fn n ->
|
||||
{dr, ap} = Digital.root(n)
|
||||
IO.puts "#{n} has additive persistence #{ap} and digital root of #{dr}"
|
||||
end)
|
||||
|
||||
base = 16
|
||||
IO.puts "\nBase = #{base}"
|
||||
fmt = "~.#{base}B(#{base}) has additive persistence ~w and digital root of ~w~n"
|
||||
Enum.each(data, fn n ->
|
||||
{dr, ap} = Digital.root(n, base)
|
||||
:io.format fmt, [n, ap, dr]
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue