File tree Expand file tree Collapse file tree 4 files changed +48
-0
lines changed Expand file tree Collapse file tree 4 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 69
69
* [ Pollard's Rho algorithm] ( https://github.com/TheAlgorithms/Rust/blob/master/src/math/pollard_rho.rs )
70
70
* [ Simpson's Rule] ( https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs )
71
71
* [ 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 )
72
73
* Searching
73
74
* [ Binary Search] ( https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs )
74
75
* [ Binary Search Recursive] ( https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search_recursive.rs )
Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ These are for demonstration purposes only.
53
53
- [x] [ Pollard's Rho algorithm] ( ./src/math/pollard_rho.rs )
54
54
- [x] [ Simpson's Rule for Integration] ( ./src/math/simpson_integration.rs )
55
55
- [x] [ Fast Fourier Transform] ( ./src/math/fast_fourier_transform.rs )
56
+ - [x] [ Armstrong Number] ( ./src/math/armstrong_number.rs )
56
57
57
58
## [ Dynamic Programming] ( ./src/dynamic_programming )
58
59
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ mod armstrong_number;
1
2
mod baby_step_giant_step;
2
3
mod extended_euclidean_algorithm;
3
4
mod fast_fourier_transform;
@@ -17,6 +18,7 @@ mod simpson_integration;
17
18
mod square_root;
18
19
mod trial_division;
19
20
21
+ pub use self :: armstrong_number:: is_armstrong_number;
20
22
pub use self :: baby_step_giant_step:: baby_step_giant_step;
21
23
pub use self :: extended_euclidean_algorithm:: extended_euclidean_algorithm;
22
24
pub use self :: fast_fourier_transform:: {
You can’t perform that action at this time.
0 commit comments