Skip to content

Instantly share code, notes, and snippets.

@awnion
Created July 18, 2014 18:56
Show Gist options
  • Save awnion/12498d2937cba22fd322 to your computer and use it in GitHub Desktop.
Save awnion/12498d2937cba22fd322 to your computer and use it in GitHub Desktop.
parallel_fib(N) ->
Parent = self(),
spawn(fun () -> Parent ! fib(N - 1) end), % call left half parallel
X = fib(N - 2),
receive Y -> Y end, % get parallel result
X + Y.
fib(N) when N > 20 -> parallel_fib(N);
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N - 1) + fib(N - 2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment