Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Fibonacci-sequence/Free-Pascal/fibonacci-sequence.pas
Normal file
40
Task/Fibonacci-sequence/Free-Pascal/fibonacci-sequence.pas
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue