Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bnum constants; check prime in each recusrive step #24

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/orchestration/recursive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::traits::{Factorize, PrimalityTest, PrimeFactorization};
use bnum::types::U256;
use num_integer::Integer;
use num_traits::One;
use std::marker::PhantomData;

pub struct RecursivePrimeFactorization<F, P>
Expand Down Expand Up @@ -41,10 +40,9 @@ where

fn recursive_factorization(&self, mut n: U256) -> Vec<U256> {
let mut factors = vec![];
let two = U256::from(2u8);
while n.is_even() {
factors.push(two);
n /= &two;
factors.push(U256::TWO);
n /= &U256::TWO;
}
self.recursion_step(n, &mut factors, 0);
factors
Expand All @@ -57,7 +55,11 @@ where
self.max_successive_fails
]
}
if n <= U256::one() {
if n <= U256::ONE {
return;
}
if PrimeTester::is_prime(&n) {
factors.push(n);
return;
}
match self.classify_factor(Factorizer::factorize(&n), &n) {
Expand Down Expand Up @@ -94,7 +96,6 @@ enum DivisorOfN {
mod tests {
use super::*;
use crate::test_framework::prime_factorization::CheckTestBuilder;
use num_traits::Zero;

struct FakePrimeTester;

Expand All @@ -109,15 +110,15 @@ mod tests {
impl Factorize for FakeFactorizer {
fn factorize(n: &U256) -> U256 {
if n.is_even() {
return U256::from(2u8);
return U256::TWO;
}
if n % U256::from(3u8) == U256::zero() {
return U256::from(3u8);
if n % U256::THREE == U256::ZERO {
return U256::THREE;
}
if n % U256::from(5u8) == U256::zero() {
return U256::from(5u8);
if n % U256::FIVE == U256::ZERO {
return U256::FIVE;
}
n.to_owned()
*n
}
}

Expand Down
Loading