-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve.ex
49 lines (41 loc) · 1.18 KB
/
curve.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
44
45
46
47
48
49
defmodule Curve do
@doc """
Area Under Curves and Volume of Revolving a Curve
https://www.hackerrank.com/challenges/area-under-curves-and-volume-of-revolving-a-curv
"""
def f(x, arr_a, arr_b) do
Enum.zip(arr_a, arr_b)
|> Enum.reduce(0.0,
fn (iter, acc) ->
a = elem(iter, 0); b = elem(iter, 1);
acc + a * :math.pow(x, b) end)
end
def area(x_a, x_b, arr_a, arr_b) do
value = x_a * 1000..x_b * 1000
|> Enum.map(fn (x) -> f(x * 0.001, arr_a, arr_b) end)
|> Enum.reduce(0.0, fn (x, acc) -> x + acc end)
Float.round(value / 1000.0, 1)
end
def volume(x_a, x_b, arr_a, arr_b) do
value = x_a * 1000..x_b * 1000
|> Enum.map(fn (x) -> f(x * 0.001, arr_a, arr_b) end)
|> Enum.reduce(0.0, fn (x, acc) -> :math.pi * x * x + acc end)
Float.round(value / 1000.0, 1)
end
def input_arr() do
IO.gets("")
|> String.trim()
|> String.split(" ")
|> Enum.map(fn (x) -> elem(Integer.parse(x), 0) end)
end
def main() do
arr_a = input_arr()
arr_b = input_arr()
[x_a, x_b] = input_arr()
area(x_a, x_b, arr_a, arr_b)
|> IO.puts()
volume(x_a, x_b, arr_a, arr_b)
|> IO.puts()
end
end
Curve.main()