Last active
March 13, 2018 14:40
-
-
Save AnthonyMikh/9ac59a168bf5d26fc2a9d3910ab28caa to your computer and use it in GitHub Desktop.
Решение задачи №78 от UniLecs (Цифровой корень числа)
This file contains hidden or 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
fn digital_root(n: u32) -> u32 { | |
if n == 0 { return 0; } | |
let modulus = n % 9; | |
if modulus == 0 { 9 } else { modulus } | |
} | |
fn main() { | |
#![deny(overflowing_literals)] | |
assert_eq!(digital_root(65536), 7); | |
assert_eq!(digital_root(2345678), 8); | |
assert_eq!(digital_root(1111), 4); | |
assert_eq!(digital_root(999999), 9); | |
assert_eq!(digital_root(0), 0); | |
//assert_eq!(digital_root(9999999999999), 9); | |
//error: literal out of range for u32 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playground: https://play.rust-lang.org/?gist=301b89867aee17cf325330ded3bdde9a&version=stable