Created
October 24, 2015 17:55
-
-
Save remram44/35ed1a5e8c3e19b40d4e to your computer and use it in GitHub Desktop.
Rust generic function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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