works kinda a bit

This commit is contained in:
Christoph J. Scherr 2023-05-13 22:21:12 +02:00
parent 552bec665b
commit 1ddbc4abfe
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
2 changed files with 66 additions and 7 deletions

View File

@ -169,6 +169,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {}", result) println!("result is {}", result)
} }
} }
@ -184,6 +185,7 @@ pub fn main() {
println!("{:?}", vec) println!("{:?}", vec)
} }
else { else {
println!("=======================================================================");
println!("result is {:?}", vec) println!("result is {:?}", vec)
} }
} }
@ -192,6 +194,7 @@ pub fn main() {
println!("{:?}", e) println!("{:?}", e)
} }
else { else {
println!("=======================================================================");
println!("could not compute: {:?}", e) println!("could not compute: {:?}", e)
} }
} }
@ -213,6 +216,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {}", result) println!("result is {}", result)
} }
}, },
@ -222,6 +226,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {}", result) println!("result is {}", result)
} }
} }
@ -235,6 +240,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {} ({:04x})", result, result) println!("result is {} ({:04x})", result, result)
} }
} }
@ -244,6 +250,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {} ({:08x})", result, result) println!("result is {} ({:08x})", result, result)
} }
} }
@ -260,6 +267,7 @@ pub fn main() {
println!("{}", result) println!("{}", result)
} }
else { else {
println!("=======================================================================");
println!("result is {} ({:08x})", result, result) println!("result is {} ({:08x})", result, result)
} }
} }

View File

@ -28,6 +28,9 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result<Vec<u128>,
if max_prime > MAX_PRIMES { if max_prime > MAX_PRIMES {
return Err(format!("max_prime too large: {max_prime}")); 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 k_parts: Vec<(u128, u32)> = Vec::new();
let mut prime_parts: Vec<u128> = Vec::new(); let mut prime_parts: Vec<u128> = Vec::new();
// get a list of the early primes // get a list of the early primes
@ -63,10 +66,11 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result<Vec<u128>,
let mut g: u128; let mut g: u128;
let mut n = n; let mut n = n;
let mut last_n = 0; let mut last_n = 0;
if verbose {
println!("======================================================================="); println!("=======================================================================");
}
loop { loop {
assert!(n > 1); assert!(n > 1);
dbg!(&n);
if verbose { if verbose {
println!("modular exponentiation with: a={a}, k={k}, n={n}"); 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<Vec<u128>,
let akn1 = akn1 - 1; let akn1 = akn1 - 1;
if akn1 == 0 { if akn1 == 0 {
a += 1; a += 1;
continue;
} }
//assert_ne!(akn1, 0); //assert_ne!(akn1, 0);
if verbose { if verbose {
@ -93,12 +98,13 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result<Vec<u128>,
println!("g = gcd(akn1, n) = gcd({akn1}, {n}) = {g}"); println!("g = gcd(akn1, n) = gcd({akn1}, {n}) = {g}");
} }
if g == 1 { if g == 1 {
if verbose {
println!("======================================================================="); println!("=======================================================================");
//return Err(format!("P minus one does not work for this setup. Use another algorithm or choose a higher max prime.")); }
break 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 { else if g == n {
dbg!(&n);
if verbose { if verbose {
println!("g = {g} = {n} = n"); println!("g = {g} = {n} = n");
println!("bad a, using a=a+1"); println!("bad a, using a=a+1");
@ -120,13 +126,53 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result<Vec<u128>,
} }
else if is_prime(g as u64) { else if is_prime(g as u64) {
prime_parts.push(g); 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) { else if is_prime(n as u64) {
prime_parts.push(n); 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; n = g;
} }
else { 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 { if verbose {
@ -136,6 +182,11 @@ pub fn p_minus_one(n: u128, max_prime: u128, verbose: bool) -> Result<Vec<u128>,
panic!("last n is the same as current n"); 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); return Ok(prime_parts);
} }