Created
June 25, 2021 15:06
-
-
Save NickyMeuleman/872d8c7cb5c1fc74ceb547ce45e64961 to your computer and use it in GitHub Desktop.
Check if a number is an Armstrong number
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
pub fn is_armstrong_number(num: u32) -> bool { | |
let digits: Vec<_> = num | |
.to_string() | |
.chars() | |
.filter_map(|n| n.to_digit(10)) | |
.collect(); | |
let num_digits = digits.len() as u32; | |
num == digits.iter().map(|n| n.pow(num_digits)).sum() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment