Fibonacci number
Deutsch · English · Français · Suomi · Русский
The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, ???The idea is to add the two last numbers in order to produce the next value.
generation
The following implementations show the principle of how to calculate Fibonacci numbers. They lack of input checks. Depending on your preferences you might either want to generate a run-time error (e. g. by {$rangeChecks on} or utilizing system.runError), raise an exception, or simply return a bogus value indicating something went wrong.
recursive implementation
type
/// domain for Fibonacci function
/// where result is within nativeUInt
// You can not name it fibonacciDomain,
// since the Fibonacci function itself
// is defined for all whole numbers
// but the result beyond F(n) exceeds high(nativeUInt).
fibonacciLeftInverseRange =
{$ifdef CPU64} 0..93 {$else} 0..47 {$endif};
{**
implements Fibonacci sequence recursively
\param n the index of the Fibonacci number to retrieve
\returns the Fibonacci value at n
}
function fibonacci(const n: fibonacciLeftInverseRange): nativeUInt;
begin
// optimization: then part gets executed most of the time
if n > 1 then
begin
fibonacci := fibonacci(n - 2) + fibonacci(n - 1);
end
else
begin
// since the domain is restricted to non-negative integers
// we can bluntly assign the result to n
fibonacci := n;
end;
end;iterative implementation
This one is preferable for its run-time behavior.
{**
implements Fibonacci sequence iteratively
\param n the index of the Fibonacci number to calculate
\returns the Fibonacci value at n
}
function fibonacci(const n: fibonacciLeftInverseRange): nativeUInt;
type
/// more meaningful identifiers than simple integers
relativePosition = (previous, current, next);
var
/// temporary iterator variable
i: longword;
/// holds preceding fibonacci values
f: array[relativePosition] of nativeUInt;
begin
f[previous] := 0;
f[current] := 1;
// note, in Pascal for-loop-limits are inclusive
for i := 1 to n do
begin
f[next] := f[previous] + f[current];
f[previous] := f[current];
f[current] := f[next];
end;
// assign to previous, bc f[current] = f[next] for next iteration
fibonacci := f[previous];
end;lookup
While calculating the Fibonacci number every time it is needed requires almost no space, it takes a split second of time. Applications heavily relying on Fibonacci numbers definitely want to use a lookup table instead. And yet in general, do not calculate what is already a known fact. Since the Fibonacci sequence doesn’t change, actually calculating it is a textbook demonstration but not intended for production use.
Nevertheless an actual implementation is omitted here, since everyone wants to have it differently, a different flavor.
see also
- Fibonacci numbers in the on-line encyclopedia of integer sequences
- Some assembly routine which uses the C calling convention that calculates the nth Fibonacci number
- Fibonacci sequence § “Pascal” on RosettaCode.org
- GNU multiple precision arithmetic library’s functions
mpz_fib_uiandmpz_fib2_ui - Lucas number, a similar series with different initial values
- Tao Yue Solution to Fibonacci Sequence Problem
From the Free Pascal wiki, page "Fibonacci number", as it stood on 2019-10-23 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 3 people, 2014–2019
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Djzepi | Created page | |
| Djzepi | ||
| Djzepi | ||
| E-ric | ||
| E-ric | (minor) | |
| E-ric | (minor) | |
| Djzepi | ||
| Kai Burghardt | review | |
| Kai Burghardt | blow up | |
| Kai Burghardt | implementation: generation vs. lookup | |
| Kai Burghardt | generation: principle | |
| Kai Burghardt | mention mpz_fib_ui; better implementation with sub-ranges | |
| Kai Burghardt | see also: insert GMP's algorithm explanation link | |
| Kai Burghardt | generation: $R+ | |
| Kai Burghardt | generation: correct syntax error (minor) | |
| Kai Burghardt | see also: add link Lucas number | |
| Kai Burghardt | iterative implementation: type | |
| Kai Burghardt | iterative implementation: match line start number | |
| Djzepi | ||
| Kai Burghardt | replace legacy syntaxhighlight syntax; typography; more link |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
