Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Nth-root/Elixir/nth-root.elixir
Normal file
13
Task/Nth-root/Elixir/nth-root.elixir
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
defmodule RC do
|
||||
def nth_root(n, x, precision \\ 1.0e-5) do
|
||||
f = fn(prev) -> ((n - 1) * prev + x / :math.pow(prev, (n-1))) / n end
|
||||
fixed_point(f, x, precision, f.(x))
|
||||
end
|
||||
|
||||
defp fixed_point(_, guess, tolerance, next) when abs(guess - next) < tolerance, do: next
|
||||
defp fixed_point(f, _, tolerance, next), do: fixed_point(f, next, tolerance, f.(next))
|
||||
end
|
||||
|
||||
Enum.each([{2, 2}, {4, 81}, {10, 1024}, {1/2, 7}], fn {n, x} ->
|
||||
IO.puts "#{n} root of #{x} is #{RC.nth_root(n, x)}"
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue