Demonstrates high-precision integer math beyond standard 64-bit limits. This example showcases the native 'BigInteger' type, illustrating its use in calculating massive factorials, performing modular exponentiation ('ModPow') for cryptographic applications, and utilizing helper functions like 'GCD' and bit-level inspection.
<?pas
// Basic operations - initialized from strings for large values
var a := BigInteger('12345678901234567890');
var b := BigInteger('98765432109876543210');
PrintLn('a = ' + a.ToString);
PrintLn('b = ' + b.ToString);
PrintLn('a + b = ' + (a + b).ToString);
PrintLn('a * b = ' + (a * b).ToString);
PrintLn('');
// Large factorial
function Factorial(n: Integer): BigInteger;
begin
Result := BigInteger(1);
for var i := 2 to n do
Result := Result * i;
end;
PrintLn('50! = ');
var fact50 := Factorial(50);
PrintLn(fact50.ToString);
PrintLn('Digits: ' + fact50.ToString.Length.ToString);
PrintLn('');
// Power operations
var base := BigInteger(2);
var huge := base.Power(256);
PrintLn('2^256 = ');
PrintLn(huge.ToString);
PrintLn('');
// Modular arithmetic
var modVal := BigInteger('1000000007');
var modResult := a.ModPow(b, modVal);
PrintLn('a^b mod 1000000007 = ' + modResult.ToString);
// GCD - global function
var x := BigInteger(48);
var y := BigInteger(18);
PrintLn('GCD(48, 18) = ' + GCD(x, y).ToString);
// Bit operations
PrintLn('Bit length of 2^256: ' + huge.BitLength.ToString);
?>
a = 12345678901234567890 b = 98765432109876543210 a + b = 111111111011111111100 a * b = 1219326311370217952237463801111263526900 50! = 30414093201713378043612608166064768844377641568960512000000000000 Digits: 65 2^256 = 115792089237316195423570985008687907853269984665640564039457584007913129639936 a^b mod 1000000007 = 577648646 GCD(48, 18) = 6 Bit length of 2^256: 257