Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Last active March 13, 2018 14:40
Show Gist options
  • Save AnthonyMikh/9ac59a168bf5d26fc2a9d3910ab28caa to your computer and use it in GitHub Desktop.
Save AnthonyMikh/9ac59a168bf5d26fc2a9d3910ab28caa to your computer and use it in GitHub Desktop.
Решение задачи №78 от UniLecs (Цифровой корень числа)
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
}
@AnthonyMikh
Copy link
Author

AnthonyMikh commented Mar 13, 2018

@AnthonyMikh
Copy link
Author

Обоснование: http://codeforces.com/blog/entry/18286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment