rust prime factorization with struct
fn prime_factorization(mut num: i64) -> Vec<PrimeFactor> {
let mut prime_factors: Vec<i64> = Vec::new();
while num % 2 == 0 {
prime_factors.push(2);
num /= 2;
}
for i in 3..(num as f64).sqrt() as i64 + 1 {
while num % i == 0 {
prime_factors.push(i);
num /= i;
}
}
if num > 2 {
prime_factors.push(num);
}
let mut prime_factors_factors: Vec<PrimeFactor> = Vec::new();
for i in 0..prime_factors.len() {
let prime_num = prime_factors[i];
if prime_factors_factors.len() <= 0 {
let prime_factor = PrimeFactor {
num: prime_num,
factor: 1,
};
prime_factors_factors.push(prime_factor);
continue;
}
let mut found = false;
for j in 0..prime_factors_factors.len() {
let mut prime_factor_option = prime_factors_factors.get_mut(j);
match prime_factor_option {
None => {}
Some(ref mut prime_factor) => {
if prime_factor.num == prime_num {
prime_factor.set_factor(prime_factor.factor + 1);
found = true;
}
}
}
}
if !found {
prime_factors_factors.push(PrimeFactor {
num: prime_num,
factor: 1,
})
}
}
prime_factors_factors
}
struct PrimeFactor {
num: i64,
factor: i64,
}
impl PrimeFactor {
fn set_factor(&mut self, factor: i64) {
self.factor = factor;
}
}