Skip to content

Instantly share code, notes, and snippets.

@remram44
Created October 24, 2015 17:55
Show Gist options
  • Save remram44/35ed1a5e8c3e19b40d4e to your computer and use it in GitHub Desktop.
Save remram44/35ed1a5e8c3e19b40d4e to your computer and use it in GitHub Desktop.
Rust generic function
extern crate num;
use num::{Num, One, Zero};
pub fn number_of_factors<T>(target: T) -> T
where T: Num + Ord + Copy
{
let mut factor_count: T = Zero::zero();;
let mut limit: T = target;
let mut i: T = One::one();
while i < limit {
if target % i == Zero::zero() {
limit = target / i;
if limit != i {
factor_count = factor_count + One::one();
}
factor_count = factor_count + One::one();
}
i = i + One::one();
}
factor_count
}
#[test]
fn test1() {
assert_eq!(number_of_factors(9u64), 3u64);
assert_eq!(number_of_factors(12i32), 6i32);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment