Math & Numbers
BigInteger Introduction
When working with cryptographic keys, factorial calculations, or any computation involving numbers larger than 9 quintillion, primitive types overflow. BigInteger provides arbitrary-precision integer arithmetic, representing integers of any size limited only by available memory.
BigInteger
An immutable class for arbitrary-precision integers that never overflow, using method calls instead of operators for arithmetic.
Creating BigInteger
Create BigInteger from strings or primitive values.
Create.java
Replay: real traced execution (multi-file project)
// Create BigInteger
import java.math.BigInteger;
public class Create {
public static void main(String[] args) {
// From string
BigInteger big1 = new BigInteger("12345678901234567890");
System.out.println("From string: " + big1);
// From long
BigInteger big2 = BigInteger.valueOf(100);
System.out.println("From long: " + big2);
// From int (converts to long first)
int value = 42;
BigInteger big3 = BigInteger.valueOf(value);
System.out.println("From int: " + big3);
// Very large number
String largeNum = "99999999999999999999999999999999999999999999999999";
BigInteger veryBig = new BigInteger(largeNum);
System.out.println("Very large: " + veryBig);
// Constants
System.out.println("\nConstants:");
System.out.println("ZERO: " + BigInteger.ZERO);
System.out.println("ONE: " + BigInteger.ONE);
System.out.println("TWO: " + BigInteger.TWO);
System.out.println("TEN: " + BigInteger.TEN);
// From byte array
byte[] bytes = {1, 2, 3};
BigInteger fromBytes = new BigInteger(bytes);
System.out.println("\nFrom bytes: " + fromBytes);
// Negative numbers
BigInteger negative = new BigInteger("-12345");
System.out.println("\nNegative: " + negative);
// With radix (base)
BigInteger hex = new BigInteger("FF", 16);
System.out.println("Hex FF: " + hex);
BigInteger binary = new BigInteger("1111", 2);
System.out.println("Binary 1111: " + binary);
// probablePrime
BigInteger prime = BigInteger.probablePrime(20, new java.util.Random(42));
System.out.println("\nProbable prime (20 bits): " + prime);
// Convert to primitives
System.out.println("\nConvert to primitives:");
BigInteger bi = BigInteger.valueOf(12345);
System.out.println("intValue: " + bi.intValue());
System.out.println("longValue: " + bi.longValue());
System.out.println("doubleValue: " + bi.doubleValue());
// String representations
System.out.println("\nString representations:");
BigInteger num = new BigInteger("255");
System.out.println("Decimal: " + num.toString());
System.out.println("Binary: " + num.toString(2));
System.out.println("Hex: " + num.toString(16));
System.out.println("Octal: " + num.toString(8));
// Compare sizes
System.out.println("\nCompare sizes:");
System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE);
BigInteger beyondLong = new BigInteger(String.valueOf(Long.MAX_VALUE)).add(BigInteger.ONE);
System.out.println("Beyond long: " + beyondLong);
}
//help h1
// new BigInteger(string) - from string
// BigInteger.valueOf(long) - from long
// BigInteger.ZERO, ONE, TWO, TEN - constants
// new BigInteger(string, radix) - from string in base
// .intValue(), .longValue() - convert to primitive
// .toString(radix) - convert to string in base
//end
}
big1 ← 12345678901234567890, big2 ← 100, value ← 42, big3 ← 42
5public class Create {6 public static void main(String[] args) {7 // From string8 BigInteger big1→ 12345678901234567890 = new BigInteger("12345678901234567890");9 System.out.println("From string: " + big112345678901234567890);1011 // From long12 BigInteger big2→ 100 = BigInteger.valueOf(100);13 System.out.println("From long: " + big2100);1415 // From int (converts to long first)16 int value→ 42 = 42;17 BigInteger big3→ 42 = BigInteger.valueOf(value42);18 System.out.println("From int: " + big342);1920 // Very large number21 String largeNum→ 99999999999999999999999999999999999999999999999999 = "99999999999999999999999999999999999999999999999999";22 BigInteger veryBig→ 99999999999999999999999999999999999999999999999999 = new BigInteger(largeNum);23 System.out.println("Very large: " + veryBig99999999999999999999999999999999999999999999999999);2425 // Constants26 System.out.println("\nConstants:");27 System.out.println("ZERO: " + BigInteger.ZERO);28 System.out.println("ONE: " + BigInteger.ONE);29 System.out.println("TWO: " + BigInteger.TWO);30 System.out.println("TEN: " + BigInteger.TEN);3132 // From byte array33 byte[] bytes = {1, 2, 3};34 BigInteger fromBytes→ 66051 = new BigInteger(bytes);35 System.out.println("\nFrom bytes: " + fromBytes66051);3637 // Negative numbers38 BigInteger negative→ -12345 = new BigInteger("-12345");39 System.out.println("\nNegative: " + negative-12345);4041 // With radix (base)42 BigInteger hex→ 255 = new BigInteger("FF", 16);43 System.out.println("Hex FF: " + hex255);44 45 BigInteger binary→ 15 = new BigInteger("1111", 2);46 System.out.println("Binary 1111: " + binary15);4748 // probablePrime49 BigInteger prime→ 1031137 = BigInteger.probablePrime(20, new java.util.Random(42));50 System.out.println("\nProbable prime (20 bits): " + prime1031137);5152 // Convert to primitives53 System.out.println("\nConvert to primitives:");54 BigInteger bi→ 12345 = BigInteger.valueOf(12345);55 System.out.println("intValue: " + bi.intValue());56 System.out.println("longValue: " + bi.longValue());57 System.out.println("doubleValue: " + bi.doubleValue());5859 // String representations60 System.out.println("\nString representations:");61 BigInteger num→ 255 = new BigInteger("255");62 System.out.println("Decimal: " + num.toString());63 System.out.println("Binary: " + num.toString(2));64 System.out.println("Hex: " + num.toString(16));65 System.out.println("Octal: " + num.toString(8));6667 // Compare sizes68 System.out.println("\nCompare sizes:");69 System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE);70 BigInteger beyondLong→ 9223372036854775808 = new BigInteger(String.valueOf(Long.MAX_VALUE)).add(BigInteger.ONE);71 System.out.println("Beyond long: " + beyondLong9223372036854775808);72 }outputFrom string: 12345678901234567890 From long: 100 From int: 42 Very large: 99999999999999999999999999999999999999999999999999 Constants: ZERO: 0 ONE: 1 TWO: 2 TEN: 10 From bytes: 66051 Negative: -12345 Hex FF: 255 Binary 1111: 15 Probable prime (20 bits): 1031137 Convert to primitives: intValue: 12345 longValue: 12345 doubleValue: 12345.0 String representations: Decimal: 255 Binary: 11111111 Hex: ff Octal: 377 Compare sizes: Long.MAX_VALUE: 9223372036854775807 Beyond long: 9223372036854775808
Immutability
BigInteger operations return new objects rather than modifying existing ones, so always capture the return value.
Arithmetic Operations
Perform basic math using method calls instead of operators.
Arithmetic.java
Replay: real traced execution (multi-file project)
// Basic arithmetic
import java.math.BigInteger;
public class Arithmetic {
public static void main(String[] args) {
BigInteger a = new BigInteger("12345678901234567890");
BigInteger b = new BigInteger("98765432109876543210");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println();
// Addition
System.out.println("Addition:");
BigInteger sum = a.add(b);
System.out.println("a + b = " + sum);
// Subtraction
System.out.println("\nSubtraction:");
BigInteger diff = b.subtract(a);
System.out.println("b - a = " + diff);
// Multiplication
System.out.println("\nMultiplication:");
BigInteger product = a.multiply(b);
System.out.println("a * b = " + product);
// Division
System.out.println("\nDivision:");
BigInteger quotient = b.divide(a);
System.out.println("b / a = " + quotient);
// Remainder (modulo)
System.out.println("\nRemainder:");
BigInteger remainder = b.remainder(a);
System.out.println("b % a = " + remainder);
// DivideAndRemainder
System.out.println("\nDivide and remainder:");
BigInteger[] divResult = b.divideAndRemainder(a);
System.out.println("Quotient: " + divResult[0]);
System.out.println("Remainder: " + divResult[1]);
// Power
System.out.println("\nPower:");
BigInteger base = BigInteger.valueOf(2);
BigInteger power = base.pow(100);
System.out.println("2^100 = " + power);
// Negate
System.out.println("\nNegate:");
BigInteger neg = a.negate();
System.out.println("-a = " + neg);
// Absolute value
System.out.println("\nAbsolute value:");
BigInteger negative = new BigInteger("-12345");
System.out.println("abs(-12345) = " + negative.abs());
// Increment/decrement
System.out.println("\nIncrement/decrement:");
BigInteger x = BigInteger.valueOf(10);
System.out.println("x = " + x);
System.out.println("x + 1 = " + x.add(BigInteger.ONE));
System.out.println("x - 1 = " + x.subtract(BigInteger.ONE));
// Chaining operations
System.out.println("\nChaining:");
BigInteger result = BigInteger.valueOf(5)
.multiply(BigInteger.valueOf(3))
.add(BigInteger.valueOf(10))
.subtract(BigInteger.valueOf(2));
System.out.println("5 * 3 + 10 - 2 = " + result);
// Factorial example
System.out.println("\nFactorial:");
System.out.println("20! = " + factorial(20));
System.out.println("50! = " + factorial(50));
// Fibonacci example
System.out.println("\nFibonacci:");
System.out.println("fib(50) = " + fibonacci(50));
System.out.println("fib(100) = " + fibonacci(100));
}
public static BigInteger factorial(int n) {
BigInteger result = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
public static BigInteger fibonacci(int n) {
if (n <= 1) return BigInteger.valueOf(n);
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
BigInteger temp = a.add(b);
a = b;
b = temp;
}
return b;
}
//help h1
// .add(other) - addition
// .subtract(other) - subtraction
// .multiply(other) - multiplication
// .divide(other) - division
// .remainder(other) - modulo
// .pow(exp) - power
// .negate() - negation
// .abs() - absolute value
// Immutable: operations return new BigInteger
//end
}
a ← 12345678901234567890, b ← 98765432109876543210, sum ← 111111111011111111100
5public class Arithmetic {6 public static void main(String[] args) {7 BigInteger a→ 12345678901234567890 = new BigInteger("12345678901234567890");8 BigInteger b→ 98765432109876543210 = new BigInteger("98765432109876543210");9 10 System.out.println("a = " + a12345678901234567890);11 System.out.println("b = " + b98765432109876543210);12 System.out.println();1314 // Addition15 System.out.println("Addition:");16 BigInteger sum→ 111111111011111111100 = a.add(b98765432109876543210);17 System.out.println("a + b = " + sum111111111011111111100);1819 // Subtraction20 System.out.println("\nSubtraction:");21 BigInteger diff→ 86419753208641975320 = b.subtract(a12345678901234567890);22 System.out.println("b - a = " + diff86419753208641975320);2324 // Multiplication25 System.out.println("\nMultiplication:");26 BigInteger product→ 1219326311370217952237463801111263526900 = a.multiply(b98765432109876543210);27 System.out.println("a * b = " + product1219326311370217952237463801111263526900);2829 // Division30 System.out.println("\nDivision:");31 BigInteger quotient→ 8 = b.divide(a12345678901234567890);32 System.out.println("b / a = " + quotient8);3334 // Remainder (modulo)35 System.out.println("\nRemainder:");36 BigInteger remainder→ 900000000090 = b.remainder(a12345678901234567890);37 System.out.println("b % a = " + remainder900000000090);3839 // DivideAndRemainder40 System.out.println("\nDivide and remainder:");41 BigInteger[] divResult = b.divideAndRemainder(a12345678901234567890);42 System.out.println("Quotient: " + divResult[0]8);43 System.out.println("Remainder: " + divResult[1]900000000090);4445 // Power46 System.out.println("\nPower:");47 BigInteger base→ 2 = BigInteger.valueOf(2);48 BigInteger power→ 1267650600228229401496703205376 = base.pow(100);49 System.out.println("2^100 = " + power1267650600228229401496703205376);5051 // Negate52 System.out.println("\nNegate:");53 BigInteger neg→ -12345678901234567890 = a.negate();54 System.out.println("-a = " + neg-12345678901234567890);5556 // Absolute value57 System.out.println("\nAbsolute value:");58 BigInteger negative→ -12345 = new BigInteger("-12345");59 System.out.println("abs(-12345) = " + negative.abs());6061 // Increment/decrement62 System.out.println("\nIncrement/decrement:");63 BigInteger x→ 10 = BigInteger.valueOf(10);64 System.out.println("x = " + x10);65 System.out.println("x + 1 = " + x.add(BigInteger.ONE));66 System.out.println("x - 1 = " + x.subtract(BigInteger.ONE));6768 // Chaining operations69 System.out.println("\nChaining:");70 BigInteger result→ 23 = BigInteger.valueOf(5)71 .multiply(BigInteger.valueOf(3))72 .add(BigInteger.valueOf(10))73 .subtract(BigInteger.valueOf(2));74 System.out.println("5 * 3 + 10 - 2 = " + result23);7576 // Factorial example77 System.out.println("\nFactorial:");78 System.out.println("20! = " + factorial(20));79 System.out.println("50! = " + factorial(50));outputa = 12345678901234567890 b = 98765432109876543210 Addition: a + b = 111111111011111111100 Subtraction: b - a = 86419753208641975320 Multiplication: a * b = 1219326311370217952237463801111263526900 Division: b / a = 8 Remainder: b % a = 900000000090 Divide and remainder: Quotient: 8 Remainder: 900000000090 Power: 2^100 = 1267650600228229401496703205376 Negate: -a = -12345678901234567890 Absolute value: abs(-12345) = 12345 Increment/decrement: x = 10 x + 1 = 11 x - 1 = 9 Chaining: 5 * 3 + 10 - 2 = 23 Factorial:result ← 1
pass 1 of 285}86public static BigInteger factorial(int n20) {87 BigInteger result→ 1 = BigInteger.ONE;88 for (int i = 2; i <= n; i++) {result ← 2
pass 1 of 6887BigInteger result = BigInteger.ONE;88for (int i2 = 2; i <= n20; i++) {89 result→ 2 = result.multiply(BigInteger.valueOf(i2));90}68 passes — pass 1 is the card above pass inresult1 2 20 2 2 3 20 6 3 4 20 24 4 5 20 120 5 6 20 720 6 7 20 5040 7 8 20 40320 8 9 20 362880 9 10 20 3628800 ⋯ 57 more passes ⋯ 67 49 50 608281864034267560872252163321295376887552831379210240000000000 68 50 50 30414093201713378043612608166064768844377641568960512000000000000 return result;
90 }91 return result2432902008176640000;92}System.out.println("20! = " + factorial(20));
77System.out.println("\nFactorial:");78System.out.println("20! = " + factorial(20));79System.out.println("50! = " + factorial(50));output20! = 2432902008176640000result ← 1
pass 2 of 285}86public static BigInteger factorial(int n50) {87 BigInteger result→ 1 = BigInteger.ONE;88 for (int i = 2; i <= n; i++) {return result;
90 }91 return result30414093201713378043612608166064768844377641568960512000000000000;92}System.out.println("50! = " + factorial(50));
78System.out.println("20! = " + factorial(20));79System.out.println("50! = " + factorial(50));8081// Fibonacci example82System.out.println("\nFibonacci:");83System.out.println("fib(50) = " + fibonacci(50));84System.out.println("fib(100) = " + fibonacci(100));output50! = 30414093201713378043612608166064768844377641568960512000000000000 Fibonacci:a ← 0, b ← 1
pass 1 of 292}93public static BigInteger fibonacci(int n50) {94 if (n <= 1) return BigInteger.valueOf(n);95 96 BigInteger a→ 0 = BigInteger.ZERO;97 BigInteger b→ 1 = BigInteger.ONE;temp ← 1, a ← 1, b ← 1
pass 1 of 14899for (int i2 = 2; i <= n50; i++) {100 BigInteger temp→ 1 = a.add(b1);101 a→ 1 = b1;102 b→ 1 = temp1;103}148 passes — pass 1 is the card above pass intempab1 2 50 1 1 1 2 3 50 2 1 1 → 2 3 4 50 3 2 2 → 3 4 5 50 5 3 3 → 5 5 6 50 8 5 5 → 8 6 7 50 13 8 8 → 13 7 8 50 21 13 13 → 21 8 9 50 34 21 21 → 34 9 10 50 55 34 34 → 55 ⋯ 137 more passes ⋯ 147 99 100 218922995834555169026 135301852344706746049 135301852344706746049 → 218922995834555169026 148 100 100 354224848179261915075 218922995834555169026 218922995834555169026 → 354224848179261915075 return b;
103 }104 return b12586269025;105}System.out.println("fib(50) = " + fibonacci(50));
82 System.out.println("\nFibonacci:");83 System.out.println("fib(50) = " + fibonacci(50));84 System.out.println("fib(100) = " + fibonacci(100));85}outputfib(50) = 12586269025a ← 0, b ← 1
pass 2 of 292}93public static BigInteger fibonacci(int n100) {94 if (n <= 1) return BigInteger.valueOf(n);95 96 BigInteger a→ 0 = BigInteger.ZERO;97 BigInteger b→ 1 = BigInteger.ONE;return b;
103 }104 return b354224848179261915075;105}System.out.println("fib(100) = " + fibonacci(100));
83 System.out.println("fib(50) = " + fibonacci(50));84 System.out.println("fib(100) = " + fibonacci(100));85}outputfib(100) = 354224848179261915075
Comparison Operations
Compare BigInteger values using methods.
Comparison.java
Replay: real traced execution (multi-file project)
// Comparison operations
import java.math.BigInteger;
public class Comparison {
public static void main(String[] args) {
BigInteger a = new BigInteger("100");
BigInteger b = new BigInteger("200");
BigInteger c = new BigInteger("100");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println();
// compareTo
System.out.println("compareTo:");
System.out.println("a.compareTo(b): " + a.compareTo(b)); // -1
System.out.println("b.compareTo(a): " + b.compareTo(a)); // 1
System.out.println("a.compareTo(c): " + a.compareTo(c)); // 0
// equals
System.out.println("\nequals:");
System.out.println("a.equals(b): " + a.equals(b));
System.out.println("a.equals(c): " + a.equals(c));
// Comparison helpers
System.out.println("\nComparison helpers:");
System.out.println("a < b: " + (a.compareTo(b) < 0));
System.out.println("a <= b: " + (a.compareTo(b) <= 0));
System.out.println("a > b: " + (a.compareTo(b) > 0));
System.out.println("a >= b: " + (a.compareTo(b) >= 0));
System.out.println("a == c: " + (a.compareTo(c) == 0));
// max and min
System.out.println("\nmax and min:");
System.out.println("max(a, b): " + a.max(b));
System.out.println("min(a, b): " + a.min(b));
// signum
System.out.println("\nsignum:");
System.out.println("signum(100): " + a.signum());
System.out.println("signum(-100): " + a.negate().signum());
System.out.println("signum(0): " + BigInteger.ZERO.signum());
// Find max in array
System.out.println("\nFind max in array:");
BigInteger[] numbers = {
new BigInteger("12345"),
new BigInteger("98765"),
new BigInteger("45678"),
new BigInteger("23456")
};
BigInteger max = findMax(numbers);
BigInteger min = findMin(numbers);
System.out.println("Max: " + max);
System.out.println("Min: " + min);
// Sort array
System.out.println("\nSort array:");
java.util.Arrays.sort(numbers);
System.out.println("Sorted: " + java.util.Arrays.toString(numbers));
// Check ranges
System.out.println("\nCheck ranges:");
BigInteger value = new BigInteger("150");
BigInteger lower = new BigInteger("100");
BigInteger upper = new BigInteger("200");
boolean inRange = value.compareTo(lower) >= 0 && value.compareTo(upper) <= 0;
System.out.println(value + " in range [" + lower + ", " + upper + "]: " + inRange);
// Zero check
System.out.println("\nZero check:");
System.out.println("a equals ZERO: " + a.equals(BigInteger.ZERO));
System.out.println("ZERO equals ZERO: " + BigInteger.ZERO.equals(BigInteger.ZERO));
// Sign check
System.out.println("\nSign check:");
BigInteger pos = new BigInteger("100");
BigInteger neg = new BigInteger("-100");
System.out.println("pos > 0: " + (pos.signum() > 0));
System.out.println("neg < 0: " + (neg.signum() < 0));
System.out.println("ZERO == 0: " + (BigInteger.ZERO.signum() == 0));
}
public static BigInteger findMax(BigInteger[] arr) {
BigInteger max = arr[0];
for (BigInteger val : arr) {
max = max.max(val);
}
return max;
}
public static BigInteger findMin(BigInteger[] arr) {
BigInteger min = arr[0];
for (BigInteger val : arr) {
min = min.min(val);
}
return min;
}
//help h1
// .compareTo(other) - returns -1, 0, or 1
// .equals(other) - equality check
// .max(other), .min(other)
// .signum() - sign (-1, 0, or 1)
// Use compareTo for ordering
// Use equals for equality
//end
}
a ← 100, b ← 200, c ← 100
5public class Comparison {6 public static void main(String[] args) {7 BigInteger a→ 100 = new BigInteger("100");8 BigInteger b→ 200 = new BigInteger("200");9 BigInteger c→ 100 = new BigInteger("100");10 11 System.out.println("a = " + a100);12 System.out.println("b = " + b200);13 System.out.println("c = " + c100);14 System.out.println();1516 // compareTo17 System.out.println("compareTo:");18 System.out.println("a.compareTo(b): " + a.compareTo(b200)); // -119 System.out.println("b.compareTo(a): " + b.compareTo(a100)); // 120 System.out.println("a.compareTo(c): " + a.compareTo(c100)); // 02122 // equals23 System.out.println("\nequals:");24 System.out.println("a.equals(b): " + a.equals(b200));25 System.out.println("a.equals(c): " + a.equals(c100));2627 // Comparison helpers28 System.out.println("\nComparison helpers:");29 System.out.println("a < b: " + (a.compareTo(b200) < 0));30 System.out.println("a <= b: " + (a.compareTo(b200) <= 0));31 System.out.println("a > b: " + (a.compareTo(b200) > 0));32 System.out.println("a >= b: " + (a.compareTo(b200) >= 0));33 System.out.println("a == c: " + (a.compareTo(c100) == 0));3435 // max and min36 System.out.println("\nmax and min:");37 System.out.println("max(a, b): " + a.max(b200));38 System.out.println("min(a, b): " + a.min(b200));3940 // signum41 System.out.println("\nsignum:");42 System.out.println("signum(100): " + a.signum());43 System.out.println("signum(-100): " + a.negate().signum());44 System.out.println("signum(0): " + BigInteger.ZERO.signum());4546 // Find max in array47 System.out.println("\nFind max in array:");48 BigInteger[] numbers = {49 new BigInteger("12345"),50 new BigInteger("98765"),51 new BigInteger("45678"),52 new BigInteger("23456")53 };54 55 BigInteger max = findMax(numbers);56 BigInteger min = findMin(numbers);outputa = 100 b = 200 c = 100 compareTo: a.compareTo(b): -1 b.compareTo(a): 1 a.compareTo(c): 0 equals: a.equals(b): false a.equals(c): true Comparison helpers: a < b: true a <= b: true a > b: false a >= b: false a == c: true max and min: max(a, b): 200 min(a, b): 100 signum: signum(100): 1 signum(-100): -1 signum(0): 0 Find max in array:max ← 12345
86}87public static BigInteger findMax(BigInteger[] arr) {88 BigInteger max→ 12345 = arr[0]12345;89 for (BigInteger val : arr) {max ← 12345
pass 1 of 488BigInteger max = arr[0];89for (BigInteger val12345 : arr) {90 max→ 12345 = max.max(val12345);91}All 4 passes — pass 1 is the card above pass valmax1 12345 12345 2 98765 98765 3 45678 98765 4 23456 98765 return max;
91 }92 return max98765;93}max ← 98765
55BigInteger max→ 98765 = findMax(numbers);56BigInteger min = findMin(numbers);57System.out.println("Max: " + max);min ← 12345
93}94public static BigInteger findMin(BigInteger[] arr) {95 BigInteger min→ 12345 = arr[0]12345;96 for (BigInteger val : arr) {min ← 12345
pass 1 of 495BigInteger min = arr[0];96for (BigInteger val12345 : arr) {97 min→ 12345 = min.min(val12345);98}All 4 passes — pass 1 is the card above pass valmin1 12345 12345 2 98765 12345 3 45678 12345 4 23456 12345 return min;
98 }99 return min12345;100}min ← 12345, value ← 150, lower ← 100, upper ← 200, inRange ← true
55 BigInteger max = findMax(numbers);56 BigInteger min→ 12345 = findMin(numbers);57 System.out.println("Max: " + max98765);58 System.out.println("Min: " + min12345);5960 // Sort array61 System.out.println("\nSort array:");62 java.util.Arrays.sort(numbers);63 System.out.println("Sorted: " + java.util.Arrays.toString(numbers));6465 // Check ranges66 System.out.println("\nCheck ranges:");67 BigInteger value→ 150 = new BigInteger("150");68 BigInteger lower→ 100 = new BigInteger("100");69 BigInteger upper→ 200 = new BigInteger("200");70 71 boolean inRange→ true = value.compareTo(lower100) >= 0 && value.compareTo(upper200) <= 0;72 System.out.println(value150 + " in range [" + lower100 + ", " + upper200 + "]: " + inRangetrue);7374 // Zero check75 System.out.println("\nZero check:");76 System.out.println("a equals ZERO: " + a.equals(BigInteger.ZERO));77 System.out.println("ZERO equals ZERO: " + BigInteger.ZERO.equals(BigInteger.ZERO));7879 // Sign check80 System.out.println("\nSign check:");81 BigInteger pos→ 100 = new BigInteger("100");82 BigInteger neg→ -100 = new BigInteger("-100");83 System.out.println("pos > 0: " + (pos.signum() > 0));84 System.out.println("neg < 0: " + (neg.signum() < 0));85 System.out.println("ZERO == 0: " + (BigInteger.ZERO.signum() == 0));86}outputMax: 98765 Min: 12345 Sort array: Sorted: [12345, 23456, 45678, 98765] Check ranges: 150 in range [100, 200]: true Zero check: a equals ZERO: false ZERO equals ZERO: true Sign check: pos > 0: true neg < 0: true ZERO == 0: true
Modular Arithmetic
Operations useful for cryptography and number theory.
Modular.java
Replay: real traced execution (multi-file project)
// Modular arithmetic
import java.math.BigInteger;
public class Modular {
public static void main(String[] args) {
String modInput = "17";
BigInteger value = new BigInteger(modInput);
BigInteger modulus = new BigInteger("5");
System.out.println("value = " + value);
System.out.println("modulus = " + modulus);
System.out.println();
// Mod
System.out.println("mod:");
BigInteger mod = value.mod(modulus);
System.out.println(value + " mod " + modulus + " = " + mod);
// ModPow (modular exponentiation)
System.out.println("\nModPow (modular exponentiation):");
BigInteger base = new BigInteger("3");
BigInteger exponent = new BigInteger("4");
BigInteger m = new BigInteger("5");
BigInteger modPow = base.modPow(exponent, m);
System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);
// ModInverse
System.out.println("\nModInverse:");
BigInteger a = new BigInteger("3");
BigInteger mod2 = new BigInteger("11");
BigInteger inverse = a.modInverse(mod2);
System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
System.out.println("Verify: " + a.multiply(inverse).mod(mod2));
// GCD
System.out.println("\nGCD:");
BigInteger x = new BigInteger("48");
BigInteger y = new BigInteger("18");
BigInteger gcd = x.gcd(y);
System.out.println("gcd(" + x + ", " + y + ") = " + gcd);
// Check coprime
System.out.println("\nCheck coprime:");
BigInteger p = new BigInteger("15");
BigInteger q = new BigInteger("28");
boolean coprime = p.gcd(q).equals(BigInteger.ONE);
System.out.println(p + " and " + q + " are coprime: " + coprime);
// Modular exponentiation (large numbers)
System.out.println("\nLarge modular exponentiation:");
BigInteger largeBase = new BigInteger("123456789");
BigInteger largeExp = new BigInteger("987654321");
BigInteger largeMod = new BigInteger("1000000007");
BigInteger result = largeBase.modPow(largeExp, largeMod);
System.out.println("Result: " + result);
// Probability prime
System.out.println("\nProbable prime:");
BigInteger candidate = new BigInteger("101");
boolean isPrime = candidate.isProbablePrime(100);
System.out.println(candidate + " is probably prime: " + isPrime);
// Generate probable prime
System.out.println("\nGenerate probable prime:");
BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
System.out.println("Random 64-bit prime: " + prime);
// Practical: RSA key generation (simplified)
System.out.println("\nRSA-like calculation:");
BigInteger p1 = new BigInteger("61");
BigInteger p2 = new BigInteger("53");
BigInteger n = p1.multiply(p2);
BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
BigInteger e = new BigInteger("17");
BigInteger d = e.modInverse(phi);
System.out.println("p = " + p1 + ", q = " + p2);
System.out.println("n = " + n);
System.out.println("φ(n) = " + phi);
System.out.println("e = " + e);
System.out.println("d = " + d);
// Encrypt/decrypt
BigInteger message = new BigInteger("42");
BigInteger encrypted = message.modPow(e, n);
BigInteger decrypted = encrypted.modPow(d, n);
System.out.println("Message: " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
//help h1
// .mod(m) - modulo
// .modPow(exp, m) - (this^exp) mod m
// .modInverse(m) - modular multiplicative inverse
// .gcd(other) - greatest common divisor
// .isProbablePrime(certainty) - primality test
// .probablePrime(bitLength, rnd) - generate prime
// Efficient for cryptography
//end
}
// Modular arithmetic
import java.math.BigInteger;
public class Modular {
public static void main(String[] args) {
String modInput = "42";
BigInteger value = new BigInteger(modInput);
BigInteger modulus = new BigInteger("5");
System.out.println("value = " + value);
System.out.println("modulus = " + modulus);
System.out.println();
// Mod
System.out.println("mod:");
BigInteger mod = value.mod(modulus);
System.out.println(value + " mod " + modulus + " = " + mod);
// ModPow (modular exponentiation)
System.out.println("\nModPow (modular exponentiation):");
BigInteger base = new BigInteger("3");
BigInteger exponent = new BigInteger("4");
BigInteger m = new BigInteger("5");
BigInteger modPow = base.modPow(exponent, m);
System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);
// ModInverse
System.out.println("\nModInverse:");
BigInteger a = new BigInteger("3");
BigInteger mod2 = new BigInteger("11");
BigInteger inverse = a.modInverse(mod2);
System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
System.out.println("Verify: " + a.multiply(inverse).mod(mod2));
// GCD
System.out.println("\nGCD:");
BigInteger x = new BigInteger("48");
BigInteger y = new BigInteger("18");
BigInteger gcd = x.gcd(y);
System.out.println("gcd(" + x + ", " + y + ") = " + gcd);
// Check coprime
System.out.println("\nCheck coprime:");
BigInteger p = new BigInteger("15");
BigInteger q = new BigInteger("28");
boolean coprime = p.gcd(q).equals(BigInteger.ONE);
System.out.println(p + " and " + q + " are coprime: " + coprime);
// Modular exponentiation (large numbers)
System.out.println("\nLarge modular exponentiation:");
BigInteger largeBase = new BigInteger("123456789");
BigInteger largeExp = new BigInteger("987654321");
BigInteger largeMod = new BigInteger("1000000007");
BigInteger result = largeBase.modPow(largeExp, largeMod);
System.out.println("Result: " + result);
// Probability prime
System.out.println("\nProbable prime:");
BigInteger candidate = new BigInteger("101");
boolean isPrime = candidate.isProbablePrime(100);
System.out.println(candidate + " is probably prime: " + isPrime);
// Generate probable prime
System.out.println("\nGenerate probable prime:");
BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
System.out.println("Random 64-bit prime: " + prime);
// Practical: RSA key generation (simplified)
System.out.println("\nRSA-like calculation:");
BigInteger p1 = new BigInteger("61");
BigInteger p2 = new BigInteger("53");
BigInteger n = p1.multiply(p2);
BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
BigInteger e = new BigInteger("17");
BigInteger d = e.modInverse(phi);
System.out.println("p = " + p1 + ", q = " + p2);
System.out.println("n = " + n);
System.out.println("φ(n) = " + phi);
System.out.println("e = " + e);
System.out.println("d = " + d);
// Encrypt/decrypt
BigInteger message = new BigInteger("42");
BigInteger encrypted = message.modPow(e, n);
BigInteger decrypted = encrypted.modPow(d, n);
System.out.println("Message: " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
//help h1
// .mod(m) - modulo
// .modPow(exp, m) - (this^exp) mod m
// .modInverse(m) - modular multiplicative inverse
// .gcd(other) - greatest common divisor
// .isProbablePrime(certainty) - primality test
// .probablePrime(bitLength, rnd) - generate prime
// Efficient for cryptography
//end
}
// Modular arithmetic
import java.math.BigInteger;
public class Modular {
public static void main(String[] args) {
String modInput = "101";
BigInteger value = new BigInteger(modInput);
BigInteger modulus = new BigInteger("5");
System.out.println("value = " + value);
System.out.println("modulus = " + modulus);
System.out.println();
// Mod
System.out.println("mod:");
BigInteger mod = value.mod(modulus);
System.out.println(value + " mod " + modulus + " = " + mod);
// ModPow (modular exponentiation)
System.out.println("\nModPow (modular exponentiation):");
BigInteger base = new BigInteger("3");
BigInteger exponent = new BigInteger("4");
BigInteger m = new BigInteger("5");
BigInteger modPow = base.modPow(exponent, m);
System.out.println(base + "^" + exponent + " mod " + m + " = " + modPow);
// ModInverse
System.out.println("\nModInverse:");
BigInteger a = new BigInteger("3");
BigInteger mod2 = new BigInteger("11");
BigInteger inverse = a.modInverse(mod2);
System.out.println("Inverse of " + a + " mod " + mod2 + " = " + inverse);
System.out.println("Verify: " + a.multiply(inverse).mod(mod2));
// GCD
System.out.println("\nGCD:");
BigInteger x = new BigInteger("48");
BigInteger y = new BigInteger("18");
BigInteger gcd = x.gcd(y);
System.out.println("gcd(" + x + ", " + y + ") = " + gcd);
// Check coprime
System.out.println("\nCheck coprime:");
BigInteger p = new BigInteger("15");
BigInteger q = new BigInteger("28");
boolean coprime = p.gcd(q).equals(BigInteger.ONE);
System.out.println(p + " and " + q + " are coprime: " + coprime);
// Modular exponentiation (large numbers)
System.out.println("\nLarge modular exponentiation:");
BigInteger largeBase = new BigInteger("123456789");
BigInteger largeExp = new BigInteger("987654321");
BigInteger largeMod = new BigInteger("1000000007");
BigInteger result = largeBase.modPow(largeExp, largeMod);
System.out.println("Result: " + result);
// Probability prime
System.out.println("\nProbable prime:");
BigInteger candidate = new BigInteger("101");
boolean isPrime = candidate.isProbablePrime(100);
System.out.println(candidate + " is probably prime: " + isPrime);
// Generate probable prime
System.out.println("\nGenerate probable prime:");
BigInteger prime = BigInteger.probablePrime(64, new java.util.Random(42));
System.out.println("Random 64-bit prime: " + prime);
// Practical: RSA key generation (simplified)
System.out.println("\nRSA-like calculation:");
BigInteger p1 = new BigInteger("61");
BigInteger p2 = new BigInteger("53");
BigInteger n = p1.multiply(p2);
BigInteger phi = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));
BigInteger e = new BigInteger("17");
BigInteger d = e.modInverse(phi);
System.out.println("p = " + p1 + ", q = " + p2);
System.out.println("n = " + n);
System.out.println("φ(n) = " + phi);
System.out.println("e = " + e);
System.out.println("d = " + d);
// Encrypt/decrypt
BigInteger message = new BigInteger("42");
BigInteger encrypted = message.modPow(e, n);
BigInteger decrypted = encrypted.modPow(d, n);
System.out.println("Message: " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
//help h1
// .mod(m) - modulo
// .modPow(exp, m) - (this^exp) mod m
// .modInverse(m) - modular multiplicative inverse
// .gcd(other) - greatest common divisor
// .isProbablePrime(certainty) - primality test
// .probablePrime(bitLength, rnd) - generate prime
// Efficient for cryptography
//end
}
modInput ← 17, value ← 17, modulus ← 5, mod ← 2, base ← 3, exponent ← 4
5public class Modular {6 public static void main(String[] args) {7 String modInput→ 17 = "17"; //@modInput="42", "101"8 BigInteger value→ 17 = new BigInteger(modInput);9 BigInteger modulus→ 5 = new BigInteger("5");10 11 System.out.println("value = " + value17);12 System.out.println("modulus = " + modulus5);13 System.out.println();1415 // Mod16 System.out.println("mod:");17 BigInteger mod→ 2 = value.mod(modulus5);18 System.out.println(value17 + " mod " + modulus5 + " = " + mod2);1920 // ModPow (modular exponentiation)21 System.out.println("\nModPow (modular exponentiation):");22 BigInteger base→ 3 = new BigInteger("3");23 BigInteger exponent→ 4 = new BigInteger("4");24 BigInteger m→ 5 = new BigInteger("5");25 BigInteger modPow→ 1 = base.modPow(exponent4, m5);26 System.out.println(base3 + "^" + exponent4 + " mod " + m5 + " = " + modPow1);2728 // ModInverse29 System.out.println("\nModInverse:");30 BigInteger a→ 3 = new BigInteger("3");31 BigInteger mod2→ 11 = new BigInteger("11");32 BigInteger inverse→ 4 = a.modInverse(mod211);33 System.out.println("Inverse of " + a3 + " mod " + mod211 + " = " + inverse4);34 System.out.println("Verify: " + a.multiply(inverse4).mod(mod211));3536 // GCD37 System.out.println("\nGCD:");38 BigInteger x→ 48 = new BigInteger("48");39 BigInteger y→ 18 = new BigInteger("18");40 BigInteger gcd→ 6 = x.gcd(y18);41 System.out.println("gcd(" + x48 + ", " + y18 + ") = " + gcd6);4243 // Check coprime44 System.out.println("\nCheck coprime:");45 BigInteger p→ 15 = new BigInteger("15");46 BigInteger q→ 28 = new BigInteger("28");47 boolean coprime→ true = p.gcd(q28).equals(BigInteger.ONE);48 System.out.println(p15 + " and " + q28 + " are coprime: " + coprimetrue);4950 // Modular exponentiation (large numbers)51 System.out.println("\nLarge modular exponentiation:");52 BigInteger largeBase→ 123456789 = new BigInteger("123456789");53 BigInteger largeExp→ 987654321 = new BigInteger("987654321");54 BigInteger largeMod→ 1000000007 = new BigInteger("1000000007");55 BigInteger result→ 652541198 = largeBase.modPow(largeExp987654321, largeMod1000000007);56 System.out.println("Result: " + result652541198);5758 // Probability prime59 System.out.println("\nProbable prime:");60 BigInteger candidate→ 101 = new BigInteger("101");61 boolean isPrime→ true = candidate.isProbablePrime(100);62 System.out.println(candidate101 + " is probably prime: " + isPrimetrue);6364 // Generate probable prime65 System.out.println("\nGenerate probable prime:");66 BigInteger prime→ 17659383477925775737 = BigInteger.probablePrime(64, new java.util.Random(42));67 System.out.println("Random 64-bit prime: " + prime17659383477925775737);6869 // Practical: RSA key generation (simplified)70 System.out.println("\nRSA-like calculation:");71 BigInteger p1→ 61 = new BigInteger("61");72 BigInteger p2→ 53 = new BigInteger("53");73 BigInteger n→ 3233 = p1.multiply(p253);74 BigInteger phi→ 3120 = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));75 BigInteger e→ 17 = new BigInteger("17");76 BigInteger d→ 2753 = e.modInverse(phi3120);77 78 System.out.println("p = " + p161 + ", q = " + p253);79 System.out.println("n = " + n3233);80 System.out.println("φ(n) = " + phi3120);81 System.out.println("e = " + e17);82 System.out.println("d = " + d2753);83 84 // Encrypt/decrypt85 BigInteger message→ 42 = new BigInteger("42");86 BigInteger encrypted→ 2557 = message.modPow(e17, n3233);87 BigInteger decrypted→ 42 = encrypted.modPow(d2753, n3233);88 System.out.println("Message: " + message42);89 System.out.println("Encrypted: " + encrypted2557);90 System.out.println("Decrypted: " + decrypted42);91 }outputvalue = 17 modulus = 5 mod: 17 mod 5 = 2 ModPow (modular exponentiation): 3^4 mod 5 = 1 ModInverse: Inverse of 3 mod 11 = 4 Verify: 1 GCD: gcd(48, 18) = 6 Check coprime: 15 and 28 are coprime: true Large modular exponentiation: Result: 652541198 Probable prime: 101 is probably prime: true Generate probable prime: Random 64-bit prime: 17659383477925775737 RSA-like calculation: p = 61, q = 53 n = 3233 φ(n) = 3120 e = 17 d = 2753 Message: 42 Encrypted: 2557 Decrypted: 42
modInput ← 42, value ← 42, modulus ← 5, mod ← 2, base ← 3, exponent ← 4
5public class Modular {6 public static void main(String[] args) {7 String modInput→ 42 = "42";8 BigInteger value→ 42 = new BigInteger(modInput);9 BigInteger modulus→ 5 = new BigInteger("5");10 11 System.out.println("value = " + value42);12 System.out.println("modulus = " + modulus5);13 System.out.println();1415 // Mod16 System.out.println("mod:");17 BigInteger mod→ 2 = value.mod(modulus5);18 System.out.println(value42 + " mod " + modulus5 + " = " + mod2);1920 // ModPow (modular exponentiation)21 System.out.println("\nModPow (modular exponentiation):");22 BigInteger base→ 3 = new BigInteger("3");23 BigInteger exponent→ 4 = new BigInteger("4");24 BigInteger m→ 5 = new BigInteger("5");25 BigInteger modPow→ 1 = base.modPow(exponent4, m5);26 System.out.println(base3 + "^" + exponent4 + " mod " + m5 + " = " + modPow1);2728 // ModInverse29 System.out.println("\nModInverse:");30 BigInteger a→ 3 = new BigInteger("3");31 BigInteger mod2→ 11 = new BigInteger("11");32 BigInteger inverse→ 4 = a.modInverse(mod211);33 System.out.println("Inverse of " + a3 + " mod " + mod211 + " = " + inverse4);34 System.out.println("Verify: " + a.multiply(inverse4).mod(mod211));3536 // GCD37 System.out.println("\nGCD:");38 BigInteger x→ 48 = new BigInteger("48");39 BigInteger y→ 18 = new BigInteger("18");40 BigInteger gcd→ 6 = x.gcd(y18);41 System.out.println("gcd(" + x48 + ", " + y18 + ") = " + gcd6);4243 // Check coprime44 System.out.println("\nCheck coprime:");45 BigInteger p→ 15 = new BigInteger("15");46 BigInteger q→ 28 = new BigInteger("28");47 boolean coprime→ true = p.gcd(q28).equals(BigInteger.ONE);48 System.out.println(p15 + " and " + q28 + " are coprime: " + coprimetrue);4950 // Modular exponentiation (large numbers)51 System.out.println("\nLarge modular exponentiation:");52 BigInteger largeBase→ 123456789 = new BigInteger("123456789");53 BigInteger largeExp→ 987654321 = new BigInteger("987654321");54 BigInteger largeMod→ 1000000007 = new BigInteger("1000000007");55 BigInteger result→ 652541198 = largeBase.modPow(largeExp987654321, largeMod1000000007);56 System.out.println("Result: " + result652541198);5758 // Probability prime59 System.out.println("\nProbable prime:");60 BigInteger candidate→ 101 = new BigInteger("101");61 boolean isPrime→ true = candidate.isProbablePrime(100);62 System.out.println(candidate101 + " is probably prime: " + isPrimetrue);6364 // Generate probable prime65 System.out.println("\nGenerate probable prime:");66 BigInteger prime→ 17659383477925775737 = BigInteger.probablePrime(64, new java.util.Random(42));67 System.out.println("Random 64-bit prime: " + prime17659383477925775737);6869 // Practical: RSA key generation (simplified)70 System.out.println("\nRSA-like calculation:");71 BigInteger p1→ 61 = new BigInteger("61");72 BigInteger p2→ 53 = new BigInteger("53");73 BigInteger n→ 3233 = p1.multiply(p253);74 BigInteger phi→ 3120 = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));75 BigInteger e→ 17 = new BigInteger("17");76 BigInteger d→ 2753 = e.modInverse(phi3120);77 78 System.out.println("p = " + p161 + ", q = " + p253);79 System.out.println("n = " + n3233);80 System.out.println("φ(n) = " + phi3120);81 System.out.println("e = " + e17);82 System.out.println("d = " + d2753);83 84 // Encrypt/decrypt85 BigInteger message→ 42 = new BigInteger("42");86 BigInteger encrypted→ 2557 = message.modPow(e17, n3233);87 BigInteger decrypted→ 42 = encrypted.modPow(d2753, n3233);88 System.out.println("Message: " + message42);89 System.out.println("Encrypted: " + encrypted2557);90 System.out.println("Decrypted: " + decrypted42);91 }outputvalue = 42 modulus = 5 mod: 42 mod 5 = 2 ModPow (modular exponentiation): 3^4 mod 5 = 1 ModInverse: Inverse of 3 mod 11 = 4 Verify: 1 GCD: gcd(48, 18) = 6 Check coprime: 15 and 28 are coprime: true Large modular exponentiation: Result: 652541198 Probable prime: 101 is probably prime: true Generate probable prime: Random 64-bit prime: 17659383477925775737 RSA-like calculation: p = 61, q = 53 n = 3233 φ(n) = 3120 e = 17 d = 2753 Message: 42 Encrypted: 2557 Decrypted: 42
modInput ← 101, value ← 101, modulus ← 5, mod ← 1, base ← 3, exponent ← 4
5public class Modular {6 public static void main(String[] args) {7 String modInput→ 101 = "101";8 BigInteger value→ 101 = new BigInteger(modInput);9 BigInteger modulus→ 5 = new BigInteger("5");10 11 System.out.println("value = " + value101);12 System.out.println("modulus = " + modulus5);13 System.out.println();1415 // Mod16 System.out.println("mod:");17 BigInteger mod→ 1 = value.mod(modulus5);18 System.out.println(value101 + " mod " + modulus5 + " = " + mod1);1920 // ModPow (modular exponentiation)21 System.out.println("\nModPow (modular exponentiation):");22 BigInteger base→ 3 = new BigInteger("3");23 BigInteger exponent→ 4 = new BigInteger("4");24 BigInteger m→ 5 = new BigInteger("5");25 BigInteger modPow→ 1 = base.modPow(exponent4, m5);26 System.out.println(base3 + "^" + exponent4 + " mod " + m5 + " = " + modPow1);2728 // ModInverse29 System.out.println("\nModInverse:");30 BigInteger a→ 3 = new BigInteger("3");31 BigInteger mod2→ 11 = new BigInteger("11");32 BigInteger inverse→ 4 = a.modInverse(mod211);33 System.out.println("Inverse of " + a3 + " mod " + mod211 + " = " + inverse4);34 System.out.println("Verify: " + a.multiply(inverse4).mod(mod211));3536 // GCD37 System.out.println("\nGCD:");38 BigInteger x→ 48 = new BigInteger("48");39 BigInteger y→ 18 = new BigInteger("18");40 BigInteger gcd→ 6 = x.gcd(y18);41 System.out.println("gcd(" + x48 + ", " + y18 + ") = " + gcd6);4243 // Check coprime44 System.out.println("\nCheck coprime:");45 BigInteger p→ 15 = new BigInteger("15");46 BigInteger q→ 28 = new BigInteger("28");47 boolean coprime→ true = p.gcd(q28).equals(BigInteger.ONE);48 System.out.println(p15 + " and " + q28 + " are coprime: " + coprimetrue);4950 // Modular exponentiation (large numbers)51 System.out.println("\nLarge modular exponentiation:");52 BigInteger largeBase→ 123456789 = new BigInteger("123456789");53 BigInteger largeExp→ 987654321 = new BigInteger("987654321");54 BigInteger largeMod→ 1000000007 = new BigInteger("1000000007");55 BigInteger result→ 652541198 = largeBase.modPow(largeExp987654321, largeMod1000000007);56 System.out.println("Result: " + result652541198);5758 // Probability prime59 System.out.println("\nProbable prime:");60 BigInteger candidate→ 101 = new BigInteger("101");61 boolean isPrime→ true = candidate.isProbablePrime(100);62 System.out.println(candidate101 + " is probably prime: " + isPrimetrue);6364 // Generate probable prime65 System.out.println("\nGenerate probable prime:");66 BigInteger prime→ 17659383477925775737 = BigInteger.probablePrime(64, new java.util.Random(42));67 System.out.println("Random 64-bit prime: " + prime17659383477925775737);6869 // Practical: RSA key generation (simplified)70 System.out.println("\nRSA-like calculation:");71 BigInteger p1→ 61 = new BigInteger("61");72 BigInteger p2→ 53 = new BigInteger("53");73 BigInteger n→ 3233 = p1.multiply(p253);74 BigInteger phi→ 3120 = p1.subtract(BigInteger.ONE).multiply(p2.subtract(BigInteger.ONE));75 BigInteger e→ 17 = new BigInteger("17");76 BigInteger d→ 2753 = e.modInverse(phi3120);77 78 System.out.println("p = " + p161 + ", q = " + p253);79 System.out.println("n = " + n3233);80 System.out.println("φ(n) = " + phi3120);81 System.out.println("e = " + e17);82 System.out.println("d = " + d2753);83 84 // Encrypt/decrypt85 BigInteger message→ 42 = new BigInteger("42");86 BigInteger encrypted→ 2557 = message.modPow(e17, n3233);87 BigInteger decrypted→ 42 = encrypted.modPow(d2753, n3233);88 System.out.println("Message: " + message42);89 System.out.println("Encrypted: " + encrypted2557);90 System.out.println("Decrypted: " + decrypted42);91 }outputvalue = 101 modulus = 5 mod: 101 mod 5 = 1 ModPow (modular exponentiation): 3^4 mod 5 = 1 ModInverse: Inverse of 3 mod 11 = 4 Verify: 1 GCD: gcd(48, 18) = 6 Check coprime: 15 and 28 are coprime: true Large modular exponentiation: Result: 652541198 Probable prime: 101 is probably prime: true Generate probable prime: Random 64-bit prime: 17659383477925775737 RSA-like calculation: p = 61, q = 53 n = 3233 φ(n) = 3120 e = 17 d = 2753 Message: 42 Encrypted: 2557 Decrypted: 42
Modular arithmetic
Computing remainders and modular inverses, essential for encryption algorithms like RSA.
Bit Operations
Manipulate individual bits in large integers.
Bitops.java
Replay: real traced execution (multi-file project)
// Bit operations
import java.math.BigInteger;
public class Bitops {
public static void main(String[] args) {
BigInteger a = new BigInteger("60"); // 111100 in binary
BigInteger b = new BigInteger("13"); // 001101 in binary
System.out.println("a = " + a + " (" + a.toString(2) + " binary)");
System.out.println("b = " + b + " (" + b.toString(2) + " binary)");
System.out.println();
// AND
System.out.println("Bitwise AND:");
BigInteger and = a.and(b);
System.out.println("a & b = " + and + " (" + and.toString(2) + ")");
// OR
System.out.println("\nBitwise OR:");
BigInteger or = a.or(b);
System.out.println("a | b = " + or + " (" + or.toString(2) + ")");
// XOR
System.out.println("\nBitwise XOR:");
BigInteger xor = a.xor(b);
System.out.println("a ^ b = " + xor + " (" + xor.toString(2) + ")");
// NOT
System.out.println("\nBitwise NOT:");
BigInteger not = a.not();
System.out.println("~a = " + not);
// AND NOT
System.out.println("\nAND NOT:");
BigInteger andNot = a.andNot(b);
System.out.println("a & ~b = " + andNot);
// Shift left
System.out.println("\nShift left:");
BigInteger shiftLeft = a.shiftLeft(2);
System.out.println("a << 2 = " + shiftLeft);
System.out.println("(multiply by 4): " + a.multiply(BigInteger.valueOf(4)));
// Shift right
System.out.println("\nShift right:");
BigInteger shiftRight = a.shiftRight(2);
System.out.println("a >> 2 = " + shiftRight);
System.out.println("(divide by 4): " + a.divide(BigInteger.valueOf(4)));
// Test bit
System.out.println("\nTest bit:");
for (int i = 0; i < 8; i++) {
System.out.println("Bit " + i + " of " + a + ": " + a.testBit(i));
}
// Set bit
System.out.println("\nSet bit:");
BigInteger setBit = BigInteger.ZERO.setBit(3);
System.out.println("Set bit 3: " + setBit + " (" + setBit.toString(2) + ")");
// Clear bit
System.out.println("\nClear bit:");
BigInteger clearBit = a.clearBit(2);
System.out.println("Clear bit 2 of " + a + ": " + clearBit);
// Flip bit
System.out.println("\nFlip bit:");
BigInteger flipBit = a.flipBit(0);
System.out.println("Flip bit 0 of " + a + ": " + flipBit);
// Bit count
System.out.println("\nBit count:");
System.out.println("Bit count of " + a + ": " + a.bitCount());
System.out.println("Bit length of " + a + ": " + a.bitLength());
// Lowest set bit
System.out.println("\nLowest set bit:");
System.out.println("Lowest set bit of " + a + ": " + a.getLowestSetBit());
// Check even/odd
System.out.println("\nEven/Odd:");
System.out.println(a + " is even: " + !a.testBit(0));
System.out.println(b + " is odd: " + b.testBit(0));
// Powers of 2
System.out.println("\nPowers of 2:");
for (int i = 0; i <= 10; i++) {
BigInteger power = BigInteger.ONE.shiftLeft(i);
System.out.println("2^" + i + " = " + power);
}
}
//help h1
// .and(other) - bitwise AND
// .or(other) - bitwise OR
// .xor(other) - bitwise XOR
// .not() - bitwise NOT
// .shiftLeft(n) - left shift (multiply by 2^n)
// .shiftRight(n) - right shift (divide by 2^n)
// .testBit(n) - test if bit n is set
// .setBit(n), .clearBit(n), .flipBit(n)
// .bitCount(), .bitLength()
//end
}
a ← 60, b ← 13, and ← 12, or ← 61, xor ← 49, not ← -61, andNot ← 48
5public class Bitops {6 public static void main(String[] args) {7 BigInteger a→ 60 = new BigInteger("60"); // 111100 in binary8 BigInteger b→ 13 = new BigInteger("13"); // 001101 in binary9 10 System.out.println("a = " + a60 + " (" + a.toString(2) + " binary)");11 System.out.println("b = " + b13 + " (" + b.toString(2) + " binary)");12 System.out.println();1314 // AND15 System.out.println("Bitwise AND:");16 BigInteger and→ 12 = a.and(b13);17 System.out.println("a & b = " + and12 + " (" + and.toString(2) + ")");1819 // OR20 System.out.println("\nBitwise OR:");21 BigInteger or→ 61 = a.or(b13);22 System.out.println("a | b = " + or61 + " (" + or.toString(2) + ")");2324 // XOR25 System.out.println("\nBitwise XOR:");26 BigInteger xor→ 49 = a.xor(b13);27 System.out.println("a ^ b = " + xor49 + " (" + xor.toString(2) + ")");2829 // NOT30 System.out.println("\nBitwise NOT:");31 BigInteger not→ -61 = a.not();32 System.out.println("~a = " + not-61);3334 // AND NOT35 System.out.println("\nAND NOT:");36 BigInteger andNot→ 48 = a.andNot(b13);37 System.out.println("a & ~b = " + andNot48);3839 // Shift left40 System.out.println("\nShift left:");41 BigInteger shiftLeft→ 240 = a.shiftLeft(2);42 System.out.println("a << 2 = " + shiftLeft240);43 System.out.println("(multiply by 4): " + a.multiply(BigInteger.valueOf(4)));4445 // Shift right46 System.out.println("\nShift right:");47 BigInteger shiftRight→ 15 = a.shiftRight(2);48 System.out.println("a >> 2 = " + shiftRight15);49 System.out.println("(divide by 4): " + a.divide(BigInteger.valueOf(4)));5051 // Test bit52 System.out.println("\nTest bit:");53 for (int i = 0; i < 8; i++) {outputa = 60 (111100 binary) b = 13 (1101 binary) Bitwise AND: a & b = 12 (1100) Bitwise OR: a | b = 61 (111101) Bitwise XOR: a ^ b = 49 (110001) Bitwise NOT: ~a = -61 AND NOT: a & ~b = 48 Shift left: a << 2 = 240 (multiply by 4): 240 Shift right: a >> 2 = 15 (divide by 4): 15 Test bit:for (int i = 0; i < 8; i++)
pass 1 of 852System.out.println("\nTest bit:");53for (int i0 = 0; i < 8; i++) {54 System.out.println("Bit " + i0 + " of " + a60 + ": " + a.testBit(i));55}outputBit 0 of 60: falseAll 8 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 setBit ← 8, clearBit ← 56, flipBit ← 61
57// Set bit58System.out.println("\nSet bit:");59BigInteger setBit→ 8 = BigInteger.ZERO.setBit(3);60System.out.println("Set bit 3: " + setBit8 + " (" + setBit.toString(2) + ")");6162// Clear bit63System.out.println("\nClear bit:");64BigInteger clearBit→ 56 = a.clearBit(2);65System.out.println("Clear bit 2 of " + a60 + ": " + clearBit56);6667// Flip bit68System.out.println("\nFlip bit:");69BigInteger flipBit→ 61 = a.flipBit(0);70System.out.println("Flip bit 0 of " + a60 + ": " + flipBit61);7172// Bit count73System.out.println("\nBit count:");74System.out.println("Bit count of " + a60 + ": " + a.bitCount());75System.out.println("Bit length of " + a60 + ": " + a.bitLength());7677// Lowest set bit78System.out.println("\nLowest set bit:");79System.out.println("Lowest set bit of " + a60 + ": " + a.getLowestSetBit());8081// Check even/odd82System.out.println("\nEven/Odd:");83System.out.println(a60 + " is even: " + !a.testBit(0));84System.out.println(b13 + " is odd: " + b.testBit(0));8586// Powers of 287System.out.println("\nPowers of 2:");88for (int i = 0; i <= 10; i++) {output Set bit: Set bit 3: 8 (1000) Clear bit: Clear bit 2 of 60: 56 Flip bit: Flip bit 0 of 60: 61 Bit count: Bit count of 60: 4 Bit length of 60: 6 Lowest set bit: Lowest set bit of 60: 2 Even/Odd: 60 is even: true 13 is odd: true Powers of 2:power ← 1
pass 1 of 1187System.out.println("\nPowers of 2:");88for (int i0 = 0; i <= 10; i++) {89 BigInteger power→ 1 = BigInteger.ONE.shiftLeft(i0);90 System.out.println("2^" + i0 + " = " + power1);91}output2^0 = 1All 11 passes — pass 1 is the card above pass ipower1 0 1 2 1 2 3 2 4 4 3 8 5 4 16 6 5 32 7 6 64 8 7 128 9 8 256 10 9 512 11 10 1024
@seealso bigdecimal_intro, math_functions
Exercise: Practical.java
Calculate factorial of a large number and check if a number is prime