diff --git a/src/main.rs b/src/main.rs index 94399ac..ab0cdc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -169,6 +169,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {}", result) } } @@ -184,6 +185,7 @@ pub fn main() { println!("{:?}", vec) } else { + println!("======================================================================="); println!("result is {:?}", vec) } } @@ -192,6 +194,7 @@ pub fn main() { println!("{:?}", e) } else { + println!("======================================================================="); println!("could not compute: {:?}", e) } } @@ -213,6 +216,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {}", result) } }, @@ -222,6 +226,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {}", result) } } @@ -235,6 +240,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {} ({:04x})", result, result) } } @@ -244,6 +250,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {} ({:08x})", result, result) } } @@ -260,6 +267,7 @@ pub fn main() { println!("{}", result) } else { + println!("======================================================================="); println!("result is {} ({:08x})", result, result) } } diff --git a/src/math/pm1.rs b/src/math/pm1.rs index 7a6bd72..cc81f4a 100644 --- a/src/math/pm1.rs +++ b/src/math/pm1.rs @@ -28,6 +28,9 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, if max_prime > MAX_PRIMES { return Err(format!("max_prime too large: {max_prime}")); } + if is_prime(n as u64) { + return Ok(vec![n]); + } let mut k_parts: Vec<(u128, u32)> = Vec::new(); let mut prime_parts: Vec = Vec::new(); // get a list of the early primes @@ -63,10 +66,11 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, let mut g: u128; let mut n = n; let mut last_n = 0; - println!("======================================================================="); + if verbose { + println!("======================================================================="); + } loop { assert!(n > 1); - dbg!(&n); if verbose { println!("modular exponentiation with: a={a}, k={k}, n={n}"); } @@ -83,6 +87,7 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, let akn1 = akn1 - 1; if akn1 == 0 { a += 1; + continue; } //assert_ne!(akn1, 0); if verbose { @@ -93,12 +98,13 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, println!("g = gcd(akn1, n) = gcd({akn1}, {n}) = {g}"); } if g == 1 { - println!("======================================================================="); - //return Err(format!("P minus one does not work for this setup. Use another algorithm or choose a higher max prime.")); - break + if verbose { + println!("======================================================================="); + } + return Err(format!("P minus one does not work for this setup. Use another algorithm or choose a higher max prime.")); + //return p_minus_one(n, 70, verbose); } else if g == n { - dbg!(&n); if verbose { println!("g = {g} = {n} = n"); println!("bad a, using a=a+1"); @@ -120,13 +126,53 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, } else if is_prime(g as u64) { prime_parts.push(g); + if verbose { + println!("calculating primes for {g}"); + } + let primes_of = p_minus_one(g, max_prime, false); + if primes_of.is_err() { + return primes_of; + } + if verbose { + dbg!(&primes_of); + } + prime_parts.append(&mut primes_of.unwrap()); } else if is_prime(n as u64) { prime_parts.push(n); + if verbose { + println!("calculating primes for {g}"); + } + let primes_of = p_minus_one(g, max_prime, false); + if primes_of.is_err() { + return primes_of; + } + if verbose { + dbg!(&primes_of); + } + prime_parts.append(&mut primes_of.unwrap()); n = g; } else { - println!("IN THE STUPID PART!!!!!!1"); + if verbose { + println!("calculating primes for {g} and {n}"); + } + let primes_of = p_minus_one(g, max_prime, false); + if primes_of.is_err() { + return primes_of; + } + if verbose { + dbg!(&primes_of); + } + prime_parts.append(&mut primes_of.unwrap()); + let primes_of = p_minus_one(n, max_prime, false); + if primes_of.is_err() { + return primes_of; + } + if verbose { + dbg!(&primes_of); + } + prime_parts.append(&mut primes_of.unwrap()); } } if verbose { @@ -136,6 +182,11 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result, panic!("last n is the same as current n"); } } + if prime_parts.len() == 0 { + //return Err(format!("Found no results?")); + } + prime_parts.sort(); + prime_parts.dedup(); return Ok(prime_parts); }