-
Notifications
You must be signed in to change notification settings - Fork 3
/
problem80.ex
43 lines (34 loc) · 889 Bytes
/
problem80.ex
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
ExUnit.start
require Integer
defmodule Problem80 do
use ExUnit.Case
def problem80 do
squares = 1..100 |>
Enum.map(&(&1*&1))
1..100 |>
Enum.filter(fn x ->
not Enum.member?(squares, x)
end) |>
Enum.map(&sum_of_100_digits_of_root/1) |>
Enum.sum |>
inspect |>
IO.puts
end
defp sum_of_100_digits_of_root n do
n_expanded = n*:euler_helper.int_pow(10,210)
newton_approximation(n_expanded, n_expanded ,0) |>
:euler_helper.int_to_digit_list |>
Enum.take(100) |>
Enum.sum
end
defp newton_approximation(_, xn, iteration) when iteration > 10000 do
xn
end
defp newton_approximation a, xn, iteration do
next_xn = div(xn + div(a,xn), 2)
newton_approximation a, next_xn, iteration+1
end
test "sum_of_100_digits_of_root" do
assert sum_of_100_digits_of_root(2) == 475
end
end