summary refs log tree commit diff stats
path: root/rust
diff options
context:
space:
mode:
Diffstat (limited to 'rust')
-rw-r--r--rust/armstrong-numbers/src/lib.rs16
-rw-r--r--rust/armstrong-numbers/tests/armstrong-numbers.rs8
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))
 }