diff options
author | Andinus <andinus@nand.sh> | 2021-09-08 23:02:26 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-09-08 23:02:26 +0530 |
commit | 09e7fe7c4bae239bda9290d3dca0fe51bee9211e (patch) | |
tree | 0c4a94ee4ba40e7975d4f0d920c211282118df7c /rust/armstrong-numbers | |
parent | 39a144c85aa4229345463280d4f61266350a6527 (diff) | |
download | exercism-09e7fe7c4bae239bda9290d3dca0fe51bee9211e.tar.gz |
Rust: Armstrong Numbers: Add solution
Diffstat (limited to 'rust/armstrong-numbers')
-rw-r--r-- | rust/armstrong-numbers/src/lib.rs | 16 | ||||
-rw-r--r-- | rust/armstrong-numbers/tests/armstrong-numbers.rs | 8 |
2 files changed, 15 insertions, 9 deletions
diff --git a/rust/armstrong-numbers/src/lib.rs b/rust/armstrong-numbers/src/lib.rs index 41de1c3..370cbf3 100644 --- a/rust/armstrong-numbers/src/lib.rs +++ b/rust/armstrong-numbers/src/lib.rs @@ -1,3 +1,17 @@ pub fn is_armstrong_number(num: u32) -> bool { - unimplemented!("true if {} is an armstrong number", num) + if num == 0 { + return true; + } + + let len = (num as f32).log10() as u32 + 1; + + let mut arms = 0; + let mut tmp = num; + while tmp != 0 { + let rem = tmp % 10; + arms += rem.pow(len); + tmp /= 10 + } + + return arms == num; } diff --git a/rust/armstrong-numbers/tests/armstrong-numbers.rs b/rust/armstrong-numbers/tests/armstrong-numbers.rs index 2163451..9ac51d0 100644 --- a/rust/armstrong-numbers/tests/armstrong-numbers.rs +++ b/rust/armstrong-numbers/tests/armstrong-numbers.rs @@ -6,49 +6,41 @@ fn test_zero_is_an_armstrong_number() { } #[test] -#[ignore] fn test_single_digit_numbers_are_armstrong_numbers() { assert!(is_armstrong_number(5)) } #[test] -#[ignore] fn test_there_are_no_2_digit_armstrong_numbers() { assert!(!is_armstrong_number(10)) } #[test] -#[ignore] fn test_three_digit_armstrong_number() { assert!(is_armstrong_number(153)) } #[test] -#[ignore] fn test_three_digit_non_armstrong_number() { assert!(!is_armstrong_number(100)) } #[test] -#[ignore] fn test_four_digit_armstrong_number() { assert!(is_armstrong_number(9474)) } #[test] -#[ignore] fn test_four_digit_non_armstrong_number() { assert!(!is_armstrong_number(9475)) } #[test] -#[ignore] fn test_seven_digit_armstrong_number() { assert!(is_armstrong_number(9_926_315)) } #[test] -#[ignore] fn test_seven_digit_non_armstrong_number() { assert!(!is_armstrong_number(9_926_316)) } |