Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelize Fibonacci sequence generator

I'm learning about parallelization and in one exercise I'm given a couple of algorithms that I should improve in performance. One of them is a Fibonacci sequence generator:

array[0] = 0;
array[1] = 1;
for (q = 2; q < MAX; q++) {
    array[q] = array[q−1] + array[q−2];
}

My suspicion is, that this cannot be optimized (by parallelization), since every number depends on the two preceding numbers (and therefore indirectly on all preceding numbers). How could this be parallelized?

like image 261
Jawap Avatar asked May 09 '13 14:05

Jawap


People also ask

Can you parallelize Fibonacci sequence?

using the formula f(n) = f(n − 1) + f(n − 2), where f(0) = 0 and f(1) = 1. This problem cannot be parallelized because the calculation of the Fibonacci numbers includes dependent calculations.

How do you parallelize a Fibonacci search?

The parallelism of the Fibonacci computation is T1(n)/T∞(n) = θ( ((1+sqrt(5))/2)n / n) which grows dramatically as n gets large. Therefore, even on the largest parallel computers, a modest value of n suffices to achieve near perfect linear speedup, since we have considerable parallel slackness.

What are the 5 terms of Fibonacci sequence?

It starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.

How do you solve a Fibonacci sequence?

In the Fibonacci sequence of numbers, each number in the sequence is the sum of the two numbers before it, with 0 and 1 as the first two numbers. The Fibonacci series of numbers begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.


1 Answers

The Fibonacci sequence is determined just by its first two elements; in fact, you could somehow parallelize it, although ugly:

F(n + 2) = F(n + 1) + F(n)
F(n + 3) = F(n + 1) + F(n + 2) = F(n + 1) * 2 + F(n)
F(n + 4) = F(n + 2) + F(n + 3) = F(n + 1) * 3 + F(n) * 2
F(n + 5) = F(n + 3) + F(n + 4) = F(n + 1) * 5 + F(n) * 3
F(n + 6) = F(n + 4) + F(n + 5) = F(n + 1) * 8 + F(n) * 5

Hopefully by now, you can see that:

F(n + k) = F(n + 1) * F(K) + F(n) * F(k - 1)

So after computing the first k numbers, you could use this relation to compute the next k items in the sequence, at the same time, parallelized.

You could also use the direct formula for Fibonacci numbers to compute them in parallel, but that is kind of too uncool (also might be too simple for learning purposes that it might serve).

like image 118
zw324 Avatar answered Oct 07 '22 06:10

zw324