armstrong numbers
This commit is contained in:
parent
edb9158b39
commit
fb03ef20e4
7 changed files with 240 additions and 0 deletions
12
rust/armstrong-numbers/src/lib.rs
Normal file
12
rust/armstrong-numbers/src/lib.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pub fn is_armstrong_number(num: u32) -> bool {
|
||||
if 0 == num {
|
||||
return true;
|
||||
}
|
||||
let count = (num as f64).log10().floor() as u32 + 1;
|
||||
|
||||
let sum: u32 = (1..=count)
|
||||
.map(|i| num % 10_u32.pow(i) / 10_u32.pow(i - 1))
|
||||
.map(|x| (x).pow(count))
|
||||
.sum();
|
||||
sum == num
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue