File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ //problem 30 of project euler
2
+ //digit fifth power
3
+ //n digit number has a lower limit, which is 10^(n-1) , because the first digit is greater than 1 <-----first item
4
+ //the sum of fifth power of the digits has a upper limit, which is n*10^5<10^6 <------second item
5
+ //the first item grows fast than the second item. We only need to consider the numbers with at most 6 digits,
6
+ //otherwise, there is not overlap between the first item and the second item
7
+
8
+
9
+ fn nth_power_sum_of_digits ( num : usize , n : usize ) ->usize {
10
+ let mut num : usize = num;
11
+ let mut sum : usize = 0 ;
12
+ while num > 0 {
13
+ let digit = num % 10 ;
14
+ if digit > 0 {
15
+ sum += digit. pow ( n as u32 ) ;
16
+ }
17
+ num = num / 10 ;
18
+ }
19
+ sum
20
+ }
21
+
22
+ fn main ( ) {
23
+ let mut total = 0 ;
24
+ let mut sum = 0 ;
25
+ for i in 10 ..( ( 10 as usize ) . pow ( 6 as u32 ) ) {
26
+ let j = nth_power_sum_of_digits ( i, 5 ) ;
27
+ if i==j {
28
+ println ! ( "{}" , i) ;
29
+ sum += i;
30
+ total += 1 ;
31
+ }
32
+ }
33
+
34
+ println ! ( "total={}" , total) ;
35
+ println ! ( "sum={}" , sum) ;
36
+ }
37
+
38
+
39
+
You can’t perform that action at this time.
0 commit comments