-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_1.37.exs
43 lines (35 loc) · 1.05 KB
/
exercise_1.37.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
defmodule ContinuedFraction do
def of(n, d, k), do: of(n, d, k, 1)
def of(_, _, k, i) when i > k, do: 0
def of(n, d, k, i) do
numer = n.(i)
denom = n.(i)
numer / (denom + of(n, d, k, i + 1))
end
def of_iter(n, d, k) do
iter(n, d, k, 0)
end
defp iter(_, _, 0, result), do: result
defp iter(n, d, index, result) do
numer = n.(index)
denom = d.(index) + result
iter(n, d, index - 1, numer / denom)
end
end
n = fn _ -> 1 end
d = fn _ -> 1 end
IO.puts "Trying to find: #{ 1 / 1.61803398875 }"
IO.puts "k = 2: #{ContinuedFraction.of(n, d, 2)}"
IO.puts "k = 10: #{ContinuedFraction.of(n, d, 10)}"
IO.puts "k = 20: #{ContinuedFraction.of(n, d, 20)}"
IO.puts "k = 30: #{ContinuedFraction.of(n, d, 30)}"
IO.puts "k = 40: #{ContinuedFraction.of(n, d, 40)}"
ExUnit.start
defmodule ContinuedFractionTests do
use ExUnit.Case, async: true
test "recursive and iterative approaches are equivalent" do
n = fn _ -> 1 end
d = fn _ -> 1 end
assert ContinuedFraction.of(n, d, 30) == ContinuedFraction.of_iter(n, d, 30)
end
end