CFLib.org – Common Function Library Project

FibCalc(x, y, top)

Last updated June 26, 2002

Version: 1 | Requires: CF5 | Library: MathLib

Description:
This script Calculates the Fibonacci sequence (each integer is the sum of the two previous integers). Note that the function will also calculate the sequence for non-Fibonacci numbers.

Return Values:
Returns a comma-delimited list of numeric values.

Example:

``````<cfoutput>
#fibcalc(0,1,200)#<BR>
#fibcalc(1,1,200)#
</cfoutput>``````

Parameters:

Name Description Required
x First number in the Fibonacci sequence. Yes
y Second number in the Fibonacci sequence. Yes
top Ceiling for the Fibonacci number. The sequence will terminate when this value is reached. Yes

Full UDF Source:

``````/**
* This script Calculates the Fibonacci sequence  (each integer is the sum of the two previous integers).
*
* @param x 	 First number in the Fibonacci sequence. (Required)
* @param y 	 Second number in the Fibonacci sequence. (Required)
* @param top 	 Ceiling for the Fibonacci number.  The sequence will terminate when this value is reached. (Required)
* @return Returns a comma-delimited list of numeric values.
* @author Phillip B. Holmes (pholmes@mediares.com)
* @version 1, June 26, 2002
*/
function FibCalc(x,y,top){
var sequence = arrayNew(1);
var total = x+y;
sequence[1] = x;
sequence[2] = y;
while (total LTE top) {
ArrayAppend(sequence, total);
x=y;
y=total;
total=(x+y);
}
return ArrayToList(sequence, ',');
}``````

date2ExcelDate
May 5, 2016

CapFirst
April 25, 2016