-module(fib). -compile([export_all]). fib0(0) -> 0; fib0(1) -> 1; fib0(N) -> fib0(N-1) + fib0(N-2). fib1(0) -> 0; fib1(1) -> 1; fib1(N) -> Self = self(), spawn(fun() -> Self ! fib0(N-1) end), spawn(fun() -> Self ! fib0(N-2) end), receive F1 -> receive F2 -> F1 + F2 end end. fib2(0) -> 0; fib2(1) -> 1; fib2(N) -> Self = self(), spawn(fun() -> Self ! fib2(N-1) end), spawn(fun() -> Self ! fib2(N-2) end), receive F1 -> receive F2 -> F1 + F2 end end. fib3(0) -> 0; fib3(1) -> 1; fib3(N) -> Self = self(), spawn(fun() -> Self ! fib1(N-1) end), spawn(fun() -> Self ! fib1(N-2) end), receive F1 -> receive F2 -> F1 + F2 end end. fib4(0, _) -> 0; fib4(1, _) -> 1; fib4(N, M) when N >= M -> Self = self(), spawn(fun() -> Self ! fib4(N-1, M) end), spawn(fun() -> Self ! fib4(N-2, M) end), receive F1 -> receive F2 -> F1 + F2 end end; fib4(N, M) when N < M -> Self = self(), spawn(fun() -> Self ! fib0(N-1) end), spawn(fun() -> Self ! fib0(N-2) end), receive F1 -> receive F2 -> F1 + F2 end end. fib4_5(N) -> fib4(N, N-4). fib4_6(N) -> fib4(N, N-5). fib4_7(N) -> fib4(N, N-6). fib4_8(N) -> fib4(N, N-7). fib4_9(N) -> fib4(N, N-8). fib4_10(N) -> fib4(N, N-9). fib5(0) -> 0; fib5(1) -> 1; fib5(N) -> fib_bottom(0, 1, 2, N). fib_bottom(L, H, C, N) when C >= N -> L+H; fib_bottom(L, H, C, N) -> fib_bottom(H, L+H, C+1, N).