It uses a known formla to evaluate a position instead of calculating it in a sequencial and/or recursive approach.
Last active
August 24, 2017 07:57
-
-
Save fschuindt/525643703ad4aba8852cf897553d13f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ConcurrentFibonacci do | |
def start do | |
parallel_map(1..1200, fn(x) -> | |
"Fibonacci of #{x} is: #{FibonacciCalculus.of(x)}" | |
end) | |
end | |
def parallel_map(list, func) do | |
list |> Enum.map(fn e -> spawn(fn -> | |
IO.puts func.(e) | |
end) end) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule FibonacciCalculus do | |
@golden_n :math.sqrt(5) | |
def of(n) do | |
(x_of(n) - y_of(n)) / @golden_n | |
end | |
def x_of(n) do | |
:math.pow((1 + @golden_n) / 2, n) | |
end | |
def y_of(n) do | |
:math.pow((1 - @golden_n) / 2, n) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment