Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -9,9 +9,47 @@ Program FastFibonacciGMP;
|
|||
// AI Assistant: DeepSeek
|
||||
// Description: Multiple Fibonacci algorithms with arbitrary precision
|
||||
// AI translated from my own 2016 python app ( the hybrid part )
|
||||
// License: Public Domain for Rosetta Code contribution
|
||||
//
|
||||
// LICENSE & ATTRIBUTION:
|
||||
// ----------------------------------------------------------------------------
|
||||
// 1. This Pascal source file (`FastFibonacciGMP.pas`) is released to the
|
||||
// PUBLIC DOMAIN for Rosetta Code contribution. Author waives all copyright.
|
||||
//
|
||||
// 2. This program uses the Free Pascal `GMP` unit, which is a binding to the
|
||||
// GNU MP Library (GMP). The `GMP` unit is part of Free Pascal's RTL-extra
|
||||
// package, licensed under the GNU Lesser General Public License (LGPL)
|
||||
// with a static linking exception.
|
||||
//
|
||||
// 3. The underlying GNU MP Library (libgmp) itself is licensed under:
|
||||
// - GNU Lesser General Public License version 3 or later (LGPLv3+), OR
|
||||
// - GNU General Public License version 2 or later (GPLv2+).
|
||||
//
|
||||
// 4. When compiled, this program links to the external GMP C library.
|
||||
// For license compliance, users must have access to the GMP library
|
||||
// source code (available at https://gmplib.org/).
|
||||
// ============================================================================
|
||||
|
||||
// MATHEMATICAL FOUNDATION ATTRIBUTION:
|
||||
// ----------------------------------------------------------------------------
|
||||
// 1. Fast Doubling Formulas (Fibonacci Squaring):
|
||||
// F(2k) = F(k) × [2×F(k+1) - F(k)]
|
||||
// F(2k+1) = F(k+1)² + F(k)²
|
||||
// Derived from Cassini's identity (1680) and Catalan's identity (1879).
|
||||
// Modern algorithmic presentation: Dijkstra (1978), Cohn (1963).
|
||||
//
|
||||
// 2. Fibonacci Tripling Formulas:
|
||||
// F(3k) = 5×F(k)³ + 3×(-1)ᵏ×F(k)
|
||||
// F(3k+1) = F(k+1)³ + 3×F(k+1)×F(k)² - F(k)³
|
||||
// Derived from Binet's formula (1843).
|
||||
// Algorithmic optimization: Various number theory sources.
|
||||
//
|
||||
// 3. Hybrid Decomposition Algorithm:
|
||||
// Recursive decomposition using factors 2 and 3 based on divisibility.
|
||||
// Original implementation concept: jpd (2016 Python implementation).
|
||||
// Ported to Pascal with algorithmic corrections: DeepSeek AI (2025).
|
||||
//
|
||||
// NOTE: These mathematical identities are in the public domain.
|
||||
// This specific implementation is original work.
|
||||
// ----------------------------------------------------------------------------
|
||||
Uses
|
||||
SysUtils,
|
||||
DateUtils,
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ function Fibo_BigInt(n: integer): string; //maXbox
|
|||
tbig2:= TInteger.create(0); //result (a)
|
||||
tbig3:= Tinteger.create(1); //b
|
||||
for it:= 1 to n do begin
|
||||
tbig1.assign(tbig2)
|
||||
tbig2.assign(tbig3);
|
||||
tbig1.add(tbig3);
|
||||
tbig3.assign(tbig1);
|
||||
end;
|
||||
tbig1.assign(tbig2)
|
||||
tbig2.assign(tbig3);
|
||||
tbig1.add(tbig3);
|
||||
tbig3.assign(tbig1);
|
||||
end;
|
||||
result:= tbig2.toString(false)
|
||||
tbig3.free;
|
||||
tbig2.free;
|
||||
|
|
|
|||
62
Task/Fibonacci-sequence/Pascal/fibonacci-sequence-7.pas
Normal file
62
Task/Fibonacci-sequence/Pascal/fibonacci-sequence-7.pas
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
program Fibonacci_console;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
function Fibonacci( n : word) : uint64;
|
||||
{
|
||||
Starts with the pair F[0],F[1]. At each iteration, uses the doubling formulae
|
||||
to pass from F[k],F[k+1] to F[2k],F[2k+1]. If the current bit of n (starting
|
||||
from the high end) is 1, there is a further step to F[2k+1],F[2k+2].
|
||||
}
|
||||
var
|
||||
marker, half_n : word;
|
||||
f, g : uint64; // pair of consecutive Fibonacci numbers
|
||||
t, u : uint64; // -----"-----
|
||||
begin
|
||||
// The values of F[0], F[1], F[2] are assumed to be known
|
||||
case n of
|
||||
0 : result := 0;
|
||||
1, 2 : result := 1;
|
||||
else begin
|
||||
half_n := n shr 1;
|
||||
marker := 1;
|
||||
while marker <= half_n do marker := marker shl 1;
|
||||
|
||||
// First time: current bit is 1 by construction,
|
||||
// so go straight from F[0],F[1] to F[1],F[2].
|
||||
f := 1; // = F[1]
|
||||
g := 1; // = F[2]
|
||||
marker := marker shr 1;
|
||||
|
||||
while marker > 1 do begin
|
||||
t := f*(2*g - f);
|
||||
u := f*f + g*g;
|
||||
if (n and marker = 0) then begin
|
||||
f := t;
|
||||
g := u;
|
||||
end
|
||||
else begin
|
||||
f := u;
|
||||
g := t + u;
|
||||
end;
|
||||
marker := marker shr 1;
|
||||
end;
|
||||
|
||||
// Last time: we need only one of the pair.
|
||||
if (n and marker = 0) then
|
||||
result := f*(2*g - f)
|
||||
else
|
||||
result := f*f + g*g;
|
||||
end; // end else (i.e. n > 2)
|
||||
end; // end case
|
||||
end;
|
||||
|
||||
// Main program
|
||||
var
|
||||
n : word;
|
||||
begin
|
||||
for n := 0 to 93 do
|
||||
WriteLn( SysUtils.Format( 'F[%2u] = %20u', [n, Fibonacci(n)]));
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue