-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargeNonMersennePrime.java
More file actions
35 lines (24 loc) · 1.13 KB
/
LargeNonMersennePrime.java
File metadata and controls
35 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package dev;
import java.math.BigInteger;
public class LargeNonMersennePrime {
public static void main(String[] args) {
// We need to find the last 10 digits of: 28433 × 2^7830457 + 1
long modulo = 10_000_000_000L; // 10^10 for last 10 digits
// Calculate 2^7830457 mod 10^10 using modular exponentiation
BigInteger base = BigInteger.valueOf(2);
BigInteger exponent = BigInteger.valueOf(7830457);
BigInteger mod = BigInteger.valueOf(modulo);
// Using BigInteger's modPow for efficient modular exponentiation
BigInteger powerMod = base.modPow(exponent, mod);
// Multiply by 28433
BigInteger result = powerMod.multiply(BigInteger.valueOf(28433));
// Add 1
result = result.add(BigInteger.ONE);
// Take mod 10^10 to get last 10 digits
result = result.mod(mod);
// Format with leading zeros if necessary
String lastTenDigits = String.format("%010d", result.longValue());
System.out.println("The last 10 digits of the prime 28433 × 2^7830457 + 1 are:");
System.out.println(lastTenDigits);
}
}