You are currently browsing the tag archive for the ‘Python’ tag.

EDX is offering for time been an really interesting course (Louv1.01x Paradigms of Computer Programming) about the theory of computer programming. One of the homework was to use tail recursion in order to calculate the Fibonacci sequence. Because the solution is really smart I like to put some Python code to show the concept.

From the mathematical point of view you can see the Fibonacci sequence some think like:

a, b, a+b, a+2b, 2a+3b, 3a+5b, 5a + 8b, 8a +13b

or
0, 1, 1, 2, 3, 5, 8, 13,

so in general: F(a, b, n) = F(b, a+b, n-1)

this can generate the following code:

```def fibonacci_calc(acc1=0, acc2=1, nummber = 0): if nummber == 0: return acc1 elif nummber == 1: return acc2 else: return fibonacci_calc(acc2, acc2+acc1, nummber-1)```

and the hole code can be called like:
`fibonacci_calc(0, 1, n)`
I wish you happy coding…

Reclame

### Comentarii recente filme5 la 990.ro livreaza malware rayfowler44310 la Because The Security World… io la 990.ro livreaza malware Ala la 990.ro livreaza malware tibor la 990.ro livreaza malware
Reclame