Several Methods to Compute Fibonacci Sequence with esProc

Uncategorized 1095 0

Fibonacci Sequence is also called “Rabbit Sequence”, because it can be described by a problem related to rabbits: After the second month of their life, a pair of rabbits can give birth to two little ones every month. If all the rabbits could live forever, then how many pairs of rabbits are there in the nth month?

During the first and the second month, there was only one pair of rabbits. Then in the third month, the first pair of little ones was born. Now there are two pairs of rabbits… In the nth month, the increased number of rabbits, compared with the number in the previous month, will equal the total number of rabbits two months earlier (i.e. the n-2th month). In this way, the total number of rabbits in each month will form an integer sequence: 1, 1, 2, 3, 5, 8, 13… From the third term, each term is the sum of the previous two numbers.

The rule of this loop calculation is that, after the first two terms 1,1, the value of a new term is found by calculating the sum of the two terms before it.

With esProc, this loop computation can be performed in the following manner:

  A B C
1 20    
2 /for n loop    
3 [1,1]    
4 for A1-2 =A3.m(-1)+A3.m(-2) >A3=A3|B4

This method uses for n loop to execute a specified number of loops. Since the values of the first and second term have been set during the initial setting, the number of loops should be reduced by two. A3 stores the Fibonacci sequence, B4 computes the sum of the last two numbers of the current sequence, and statement in C4 is used to append B4’s results into the integer sequence.

Computed results in A3 will be:

esProc_fibonacci_2

for x statement is another choice in esProc to execute loop computation. If the value of x is true, the execution of loop statement block will continue all through, as shown below:

  A B
1 20  
2 /while loop  
3 [1,1]  
4 for A3.len()<A1 >A3=A3|(A3.m(-1)+A3.m(-2))

A3 stores the Fibonacci Sequence. According to the statement in A4, loop computation will be on when the number of terms is less than the specified total number. Statement in B4, which directly calculate a new term and adds it to the integer sequence, is actually the combination of the statements in B4 and C4 in the above example. When computations are finished, results in A3 are the same as those of the above example.

Alternatively, for loop statement can be replaced in esProc by loop function to perform loop computation:

  A
1 20
2 /n.(x)
3 >a=0,b=1
4 =A1.((b=a+b,a=b-a))

During initial setting in A3, values of both the zero and first term are determined. With loop function in A4, each time when the value of next term is computed, it will be assigned to b; the original value of b, i.e., the value of current term, will be assigned to a, and at the same time, the current term of the integer sequence is reset to a. With loop parameters, the amount of code can be effectively reduced. A4 gets the same Fibonacci sequence as the one in previous examples.

Also, esProc can use subroutine to do the computations:

  A B C
1 20    
2 /func    
3 =func(A4,A1)    
4 func if A4<=2 return [1]*A4
5   =func(A4,A4-1) return B5|(B5.m(-1)+B5.m(-2))

The subroutine after A4 is responsible for computing values of specified terms in the Fibonacci sequence. if statement is used in the subroutine to make judgment, classifying the producing of Fibonacci sequence into two categories: if the number of terms is less than or equal to 2, 1 is entered into each term; if it is more than 2, call function recursively in B5 to get the first n-1 terms, compute the sum of the last two terms and add results to the integer sequence. Results of A3 are the same as those got by previous methods. 

By comparing the above several methods, we find that for loop is simple and easy to understand; that the loop function boasts the least amount of code; and that the subroutine calls function conveniently and has high reusability of the code, thus it is better at handling cases that requires doing the same computation repeatedly. In practice, we can use them as needed. 

FAVOR (0)
Leave a Reply
Cancel
Icon

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website