I am writing a program in Rust to check if large numbers are Mersenne primes for fun. For some reason, when I test the program with an exponent of 1_000_000_000 it takes around 5 seconds, but when I use a much smaller exponent of 82,589,933 (to generate the largest Mersenne prime) it never finishes processing. Do any of you know what is happening? Why would a larger exponent increase the performance of my code?
use std::str::FromStr;
use num::{BigInt, One, Zero};
fn is_prime(number: &BigInt) -> bool {
let mut iteration: BigInt = BigInt::from(2);
while iteration < *number {
if number % &iteration == Zero::zero() {
return false;
}
iteration += BigInt::one();
}
true
}
fn main() {
let exponent: u32 = 1_000_000_000; // when changed to 82,589,933 it never finishes
let number: BigInt = BigInt::from_str("2").unwrap().pow(exponent) - BigInt::one();
let is_prime: bool = is_prime(&number);
println!("2^{exponent} - 1 is prime: {}", is_prime);
}