Naive approach to Armstrong Number

This commit is contained in:
Blizzard Finnegan 2023-08-22 13:55:25 -04:00
parent 8ec359efef
commit e836626b87
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
2 changed files with 6 additions and 9 deletions

View file

@ -1,3 +1,8 @@
pub fn is_armstrong_number(num: u32) -> bool {
unimplemented!("true if {} is an armstrong number", num)
let num_vec:Vec<u32> = num.to_string().chars().filter(|c| !c.is_numeric()).map(|c| c.to_digit(10).unwrap_or(0)).collect();
let mut total = 0;
for val in num_vec{
total += val.pow(2);
}
total == num
}

View file

@ -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))
}