Friday, February 28, 2014

Simple Fibonacci Program

Previously, I’ve been working with algorithms of sequences that use the for i in (range(n)) command. I’ve learned that this command is ideal for sequences in value every single number within the this simple range list [1,2.3,4,5,6,7], that or any other sort of simple range (n, x) command. The important thing for me was to note that while is possible to solve for the fibonacci sequence using the range command, it can be simplified using the version above. The fibonacci sequence is unique in that use only requires the sum of previous two numbers. In other words, the f(1), and f(2) does not directly influence the outcome of f(5)=, for example. F(1) and f(2), rather only directly influence the outcome of f(3). It indirectly influences the outcome of f(5), because f(1) and f(2) compute the outcome of f(3) in which f(3) partially computes the outcome of f(5). Because of this, the range command was not ideal (for me at least) when calculating the fibonacci sequence. The key here, rather, was to notice this kind of indirect influence. Here, the established method to treat this sort of problem would be to call the fibonacci sequence multiple times. f(1) returns 1, since it preestablishes sequence. Next, f(2) simply follows through the else return command and returns 1. f(3), however, calls the program twice more. f(3) calls the program to reevaluate f(2) as well as + f(1), which then has to reevaluate f(1) and f(0). As the input number within the sequence increases, the number of calls and reevaluations also increase. For example, f(5) has to call the program to follow through and compute the following: f(4)+f(3), which is really branched out to f(3)+f(2) and f(2)+f(1) which is branched out to f(2)+f(1) and f(1) + f(0) which is branched out to f(1) and f(0), respectively. That’s a lot of computations!

No comments:

Post a Comment