Skip to content

Commit 48acede

Browse files
author
Sedat Aybars Nazlica
authored
Add Armstrong Numbers (rust-lang#305)
1 parent d44abff commit 48acede

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* [Pollard's Rho algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pollard_rho.rs)
7070
* [Simpson's Rule](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs)
7171
* [Fast Fourier Transform](https://github.com/TheAlgorithms/Rust/blob/master/src/math/fast_fourier_transform.rs)
72+
* [Armstrong Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/armstrong_number.rs)
7273
* Searching
7374
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)
7475
* [Binary Search Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ These are for demonstration purposes only.
5353
- [x] [Pollard's Rho algorithm](./src/math/pollard_rho.rs)
5454
- [x] [Simpson's Rule for Integration](./src/math/simpson_integration.rs)
5555
- [x] [Fast Fourier Transform](./src/math/fast_fourier_transform.rs)
56+
- [x] [Armstrong Number](./src/math/armstrong_number.rs)
5657

5758
## [Dynamic Programming](./src/dynamic_programming)
5859

src/math/armstrong_number.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub fn is_armstrong_number(number: u32) -> bool {
2+
let mut digits: Vec<u32> = Vec::new();
3+
let mut num: u32 = number;
4+
5+
loop {
6+
digits.push(num % 10);
7+
num /= 10;
8+
if num == 0 {
9+
break;
10+
}
11+
}
12+
13+
let sum_nth_power_of_digits: u32 = digits
14+
.iter()
15+
.map(|digit| digit.pow(digits.len() as u32))
16+
.sum();
17+
sum_nth_power_of_digits == number
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
24+
#[test]
25+
fn one_digit_armstrong_number() {
26+
assert!(is_armstrong_number(1))
27+
}
28+
#[test]
29+
fn two_digit_numbers_are_not_armstrong_numbers() {
30+
assert!(!is_armstrong_number(15))
31+
}
32+
#[test]
33+
fn three_digit_armstrong_number() {
34+
assert!(is_armstrong_number(153))
35+
}
36+
#[test]
37+
fn three_digit_non_armstrong_number() {
38+
assert!(!is_armstrong_number(105))
39+
}
40+
#[test]
41+
fn big_armstrong_number() {
42+
assert!(is_armstrong_number(912985153))
43+
}
44+
}

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod armstrong_number;
12
mod baby_step_giant_step;
23
mod extended_euclidean_algorithm;
34
mod fast_fourier_transform;
@@ -17,6 +18,7 @@ mod simpson_integration;
1718
mod square_root;
1819
mod trial_division;
1920

21+
pub use self::armstrong_number::is_armstrong_number;
2022
pub use self::baby_step_giant_step::baby_step_giant_step;
2123
pub use self::extended_euclidean_algorithm::extended_euclidean_algorithm;
2224
pub use self::fast_fourier_transform::{

0 commit comments

Comments
 (0)