Fibonacci

Picture by Unsplash Unsplash

 

Hello everyone! Welcome to Quiz 08’s Blog post. In this quiz we actually had to do two programs about the same thing, giving the ‘nth’ fibonacci number. This program was actually very easy to do if you had the correct information. So the first program was about doing a loop and the second one was doing a recursion, which basically is doing a function that calls itself (I know it sounds weird, but you should probably check the book, check where it says recursions).

I highly recommend reading the book (because it also gives the example of fibonacci numbers with recursions, it’s section 5.12) and also if you want your program to support a wider range of values you may want to use long long variables and also unsigned, which basically means that the variable won’t work with negative numbers; giving you more space to work with positive numbers.

So let’s get started, here are my two programs:

Captura de pantalla 2017-03-07 a la(s) 11.29.30.png

Captura de pantalla 2017-03-07 a la(s) 11.28.55.png

Captura de pantalla 2017-03-07 a la(s) 11.28.10.png

Captura de pantalla 2017-03-07 a la(s) 11.28.26.png

As you may have seen I timed both programs in order to conclude this: The recursion program may look more «elegant», since it’s smaller and also may be easier to understand, and it may occupy less space in memory. Also the loop one is more «efficient», since it takes less time to perform the calculations, and believe me, if you try with bigger numbers time will be way faster with the loop one. So in conclusion, I think that the space we will save with recursions is not that much of a difference as time, so I thing the loop one is the better one to work with.

As always my codes at GitHub:
Loop
Recursion

If you have questions, feel free to ask.

 

l.out

Deja un comentario