2014年9月1日星期一

Several Methods to Compute Fibonacci Sequence with esProc

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 pair of rabbits are there by the nth month?

During the first and the second month, there is only one pair of rabbits. Then in the third month, the first pair of little ones will be born. Now there are two pairs of rabbits... By the nth month, the increased number of rabbits, compared with the previous month, equals to the total number of rabbits two months ago ( i.e. in the n-2th month). In this way, the total number of rabbits can be expressed by a series: 1, 1, 2, 3, 5, 8, 13... From the third number, each number is the sum of previous two ones.

The principle of this loop computation is that, from the first two terms 1,1, the value of a new term is created by cyclically calculating the sum of the last two terms.

With esProc, the loop computation will be operated in the following manner:

This method uses for n loop to complete looping execution of designated times. Since the values of the first and second term have been decided during the initial setting, the loop computation should be reduced by two times. A3 stores Fibonacci Sequence, B4 computes the sum of last two numbers, statement in C4 is used to add results from B4 into the series.

Computed results in A3 will be:

Or, for x is used in esProc to execute loop computation. If the value of x is true,looping execution of statement block will continue all through, as shown in this figure:

A6 stores Fibonacci Sequence. According to the statement in A7, looping execution begins when the number of terms is less than designated total numbers. Statement in B7, which directly works out new term  to add to the series, is the combination of that in B4 and C4. When computations are finished, results in A6 and A3 are the same.

Or, for looping statement can be replaced in esProc by loop function to realize loop computation:

During initial setting in A9, values of both the zero and first term are determined. With loop function in A10, each time when the value of next term is computed, it will be assigned to b;and the original value of b, i.e., the value of current term, will be assigned to a; at the same time, the current term of the series is reset to a. With loop parameter, the amount of code can be effectively reduced. Result in A10 is the same as that in A3.

Also, subprogram of esProc is employed to make computations:
The subprogram in the latter part of A13 is responsible for computing Fibonacci Sequence of designated number of terms. if statement is used in the subprogram to make judgment, classifying two cases in producing Fibonacci Sequence: if the number of terms is less than or equal to 2, 1 is filled in each term; if it is more than 2, call function with recursion in B14 to get the first n-1 terms, compute the sum of the last two terms and add result to the series. Results of A12 and A3 are the same. 

By comparing the above several computing methods, we find that for loop is simple and easy to understand; loop function boasts the least amount of code; and for subprogram, calling is convenient and reusability of the code is high, so it’s suitable for cases required to make the same repeated computations. In real-world applications, we can make different choices as needed. 

没有评论:

发表评论