Explores advanced mathematical concepts using arbitrary-precision integers. This example showcases factorials of large numbers, efficient primality testing using the 'IsPrime' method, and the 'ModPow' function for secure numerical transformations, highlighting the efficiency of native BigInt operators.
<?pas
// 1. Calculating a large Factorial
function Factorial(n: Integer): BigInteger;
begin
var r := BigInteger(1);
for var i := 2 to n do
r := r * i;
Result := r;
end;
var f100 := Factorial(100);
PrintLn('100! = ' + f100.ToString);
PrintLn('Length: ' + IntToStr(f100.ToString.Length) + ' digits');
PrintLn('');
// 2. Primality testing with large numbers
// M31 = 2^31 - 1
var m31 := (BigInteger(1) shl 31) - 1;
PrintLn('M31 (2^31 - 1) = ' + m31.ToString);
if m31.IsPrime then
PrintLn('M31 is prime.');
// M61 = 2^61 - 1
var m61 := (BigInteger(1) shl 61) - 1;
PrintLn('M61 (2^61 - 1) = ' + m61.ToString);
if m61.IsPrime then
PrintLn('M61 is prime.');
// 3. Modular Exponentiation
// Calculate (7^123) mod 100
var v7 := BigInteger(7);
var e123 := BigInteger(123);
var m100 := BigInteger(100);
var res := v7.ModPow(e123, m100);
PrintLn('');
PrintLn('(7^123) mod 100 = ' + res.ToString);
?>
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 Length: 158 digits M31 (2^31 - 1) = 2147483647 M31 is prime. M61 (2^61 - 1) = 2305843009213693951 M61 is prime. (7^123) mod 100 = 43