DEV Community

Discussion on: Daily Challenge #78 - Number of Proper Fractions with Denominator d

Collapse
 
brightone profile image
Oleksii Filonenko • Edited

Rust:

use num::Integer;

fn proper_fractions(d: usize) -> usize {
    (1..d).filter(|n| d.gcd(n) == 1).count()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_examples() {
        assert_eq!(proper_fractions(0), 0);
        assert_eq!(proper_fractions(1), 0);
        assert_eq!(proper_fractions(2), 1);
        assert_eq!(proper_fractions(5), 4);
        assert_eq!(proper_fractions(15), 8);
        assert_eq!(proper_fractions(25), 20);
    }
}

Two beers for the num crate (for gcd) :)