mirror of
https://bitbucket.org/Mattrixwv/rustclasses.git
synced 2025-12-07 02:43:59 -05:00
Added get divisors function
This commit is contained in:
@@ -303,3 +303,76 @@ pub fn getNumPrimesBig(numberOfPrimes: num::BigInt) -> Vec<num::BigInt>{
|
||||
primes.sort();
|
||||
return primes;
|
||||
}
|
||||
|
||||
//This function returns all the divisors of goalNumber
|
||||
pub fn getDivisors(goalNumber: i64) -> Vec<i64>{
|
||||
let mut divisors = Vec::<i64>::new();
|
||||
|
||||
//Start by checking that the number is positive
|
||||
if(goalNumber <= 0){
|
||||
return divisors;
|
||||
}
|
||||
//If the number is 1 return just itself
|
||||
else if(goalNumber == 1){
|
||||
divisors.push(1);
|
||||
return divisors;
|
||||
}
|
||||
|
||||
//Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly
|
||||
let topPossibleDivisor = (goalNumber as f64).sqrt().ceil() as i64;
|
||||
let mut possibleDivisor = 1i64;
|
||||
while(possibleDivisor <= topPossibleDivisor){
|
||||
//If you find one add it and the number it creates to the list
|
||||
if((goalNumber % possibleDivisor) == 0){
|
||||
divisors.push(possibleDivisor);
|
||||
//Account for the possibility of sqrt(goalNumber) being a divisor
|
||||
if(possibleDivisor != topPossibleDivisor){
|
||||
divisors.push(goalNumber / possibleDivisor);
|
||||
}
|
||||
//Take care of a few occations where a number was added twice
|
||||
if(divisors.last().unwrap() == &(possibleDivisor + 1)){
|
||||
possibleDivisor += 1;
|
||||
}
|
||||
}
|
||||
possibleDivisor += 1;
|
||||
}
|
||||
|
||||
//Sort the list before returning it (for neatness)
|
||||
divisors.sort();
|
||||
return divisors;
|
||||
}
|
||||
pub fn getDivisorsBig(goalNumber: num::BigInt) -> Vec<num::BigInt>{
|
||||
let mut divisors = Vec::<num::BigInt>::new();
|
||||
//Start by checking that he number is positive
|
||||
if(goalNumber <= num::BigInt::from(0)){
|
||||
return divisors;
|
||||
}
|
||||
//If the number is 1 return just itself
|
||||
else if(goalNumber == num::BigInt::from(1)){
|
||||
divisors.push(num::BigInt::from(1));
|
||||
return divisors;
|
||||
}
|
||||
|
||||
//Start at 3 and loop through all numbers < sqrt(goalNumber) looking for a number that divides it evenly
|
||||
let topPossibleDivisor = goalNumber.sqrt();
|
||||
let mut possibleDivisor = num::BigInt::from(1);
|
||||
while(&possibleDivisor <= &topPossibleDivisor){
|
||||
//If you find one add it and the number it creates to the list
|
||||
if((&goalNumber % &possibleDivisor) == num::BigInt::from(0)){
|
||||
divisors.push(num::BigInt::new(possibleDivisor.sign(), possibleDivisor.to_u32_digits().1));
|
||||
//Account for the possibility of sqrt(goalNumber) being a divisor
|
||||
if(&possibleDivisor != &topPossibleDivisor){
|
||||
divisors.push(num::BigInt::new((&goalNumber / &possibleDivisor).sign(), (&goalNumber / &possibleDivisor).to_u32_digits().1));
|
||||
}
|
||||
//Take care of a few occations where the number was added twice
|
||||
if(divisors.last().unwrap() == &(&possibleDivisor + num::BigInt::from(1))){
|
||||
possibleDivisor += num::BigInt::from(1);
|
||||
}
|
||||
}
|
||||
possibleDivisor += 1;
|
||||
}
|
||||
|
||||
//Sort the list before returning it (for neatness)
|
||||
divisors.sort();
|
||||
return divisors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user