Python is pretty nice but the ability to do big integers is not unique to Python.neilrieck wrote: ↑Wed Jul 19, 2023 6:58 amBut here is a practical example: In 2012 I toyed with writing a "Diffie-Hellman Key Exchange" demo but then ran into library limitations of the AlphaServer I was on at the time (this is the point were most people would go off to write their own routines to support math larger than the maximum native size). Four years later I was learning python3 so went back to that demo and coded it with little difficulty.
Code: Select all
1) In BASIC, this statement "PRINT int(2^31)" fails with 32-bit signed integers (longword) (written for clarity; you should append % to each number) 2) In BASIC, this statement "PRINT int(2^63)" fails with 64-bit signed integers (quadword) 3) In Python, this statement "PRINT(2**9999)" instantly yields 37x80+50 characters. (now repeat by adding more nines)
Code: Select all
$ type big.py
print('Python:')
print(2**9999)
$ python big.py
Python:
997531558440379192441871081341792541911748415943096227426004474926471941511097331595998084201809729894966556471160456213577824567470
...
104201821088185195577531989450037142683609814045173898726666023418439793429011897610931456004037140977565897407881222414925923075485
2444013637360787344065797375204866057540249095227901708413474893570658031605343195755840887152396298354688
$ type big.php
<?php
echo 'PHP:' . "\r\n";
echo bcpow('2', '9999') . "\r\n";
?>
$ php big.php
PHP:
997531558440379192441871081341792541911748415943096227426004474926471941511097331595998084201809729894966556471160456213577824567470
...
104201821088185195577531989450037142683609814045173898726666023418439793429011897610931456004037140977565897407881222414925923075485
2444013637360787344065797375204866057540249095227901708413474893570658031605343195755840887152396298354688
$ type Big.java
import java.math.BigInteger;
public class Big {
public static void main(String[] args) throws Exception {
System.out.println("Java:");
System.out.println(BigInteger.valueOf(2).pow(9999));
}
}
$ javac Big.java
$ java "Big"
Java:
997531558440379192441871081341792541911748415943096227426004474926471941511097331595998084201809729894966556471160456213577824567470
...
104201821088185195577531989450037142683609814045173898726666023418439793429011897610931456004037140977565897407881222414925923075485
2444013637360787344065797375204866057540249095227901708413474893570658031605343195755840887152396298354688
$ type big.c
#include <stdio.h>
#include <gmp.h>
int main(int argc, char *argv[])
{
mpz_t val;
printf("C:\n");
mpz_init(val);
mpz_set_ui(val, 2);
mpz_pow_ui(val, val, 9999);
mpz_out_str(stdout, 10, val);
mpz_clear(val);
return 0;
}
$ cc /name=as_is /include=gmpdir /define="_GMP_H_HAVE_FILE" big
$ link big + sys$input/opt
libgmp/share
$
$ r big
C:
997531558440379192441871081341792541911748415943096227426004474926471941511097331595998084201809729894966556471160456213577824567470
...
104201821088185195577531989450037142683609814045173898726666023418439793429011897610931456004037140977565897407881222414925923075485
2444013637360787344065797375204866057540249095227901708413474893570658031605343195755840887152396298354688
$ type big.cpp
#include <iostream.h>
#include <integer.h>
int main()
{
cout << "C++:" << endl;
Integer val = 2;
val = pow(val, 9999);
cout << val << endl;
return 0;
}
$ gxx big.cpp
$ gxxlink big
$ r big
C++:
997531558440379192441871081341792541911748415943096227426004474926471941511097331595998084201809729894966556471160456213577824567470
...
104201821088185195577531989450037142683609814045173898726666023418439793429011897610931456004037140977565897407881222414925923075485
2444013637360787344065797375204866057540249095227901708413474893570658031605343195755840887152396298354688
Python, PHP and Java are builtin functionality.
C uses the widely known LIBGMP (it builds on VMS thanks to JoukJ).
C++ uses the old GNU Integer class. There are plenty of other C++ classes available with big integer functionality.