1
1
"""
2
- An Armstrong number is equal to the sum of its own digits each raised
3
- to the power of the number of digits.
2
+ An Armstrong number is equal to the sum of its own digits each raised to the
3
+ power of the number of digits.
4
+
4
5
For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
5
- An Armstrong number is often called Narcissistic number.
6
+
7
+ Armstrong numbers are also called Narcissistic numbers and Pluperfect numbers.
8
+
9
+ On-Line Encyclopedia of Integer Sequences entry: https://oeis.org/A005188
6
10
"""
11
+ PASSING = (1 , 153 , 370 , 371 , 1634 , 24678051 , 115132219018763992565095597973971522401 )
12
+ FAILING = (- 153 , - 1 , 0 , 1.2 , 200 , "A" , [], {}, None )
7
13
8
14
9
15
def armstrong_number (n : int ) -> bool :
10
16
"""
11
17
Return True if n is an Armstrong number or False if it is not.
12
18
13
- >>> armstrong_number(153 )
19
+ >>> all( armstrong_number(n) for n in PASSING )
14
20
True
15
- >>> armstrong_number(200)
16
- False
17
- >>> armstrong_number(1634)
18
- True
19
- >>> armstrong_number(0)
20
- False
21
- >>> armstrong_number(-1)
22
- False
23
- >>> armstrong_number(1.2)
21
+ >>> any(armstrong_number(n) for n in FAILING)
24
22
False
25
23
"""
26
24
if not isinstance (n , int ) or n < 1 :
@@ -43,15 +41,46 @@ def armstrong_number(n: int) -> bool:
43
41
return n == sum
44
42
45
43
46
- def narcissistic_number (n : int ) -> bool :
47
- """Return True if n is a narcissistic number or False if it is not"""
44
+ def pluperfect_number (n : int ) -> bool :
45
+ """Return True if n is a pluperfect number or False if it is not
46
+
47
+ >>> all(armstrong_number(n) for n in PASSING)
48
+ True
49
+ >>> any(armstrong_number(n) for n in FAILING)
50
+ False
51
+ """
52
+ if not isinstance (n , int ) or n < 1 :
53
+ return False
54
+
55
+ # Init a "histogram" of the digits
56
+ digit_histogram = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
57
+ digit_total = 0
58
+ sum = 0
59
+ temp = n
60
+ while temp > 0 :
61
+ temp , rem = divmod (temp , 10 )
62
+ digit_histogram [rem ] += 1
63
+ digit_total += 1
64
+
65
+ for (cnt , i ) in zip (digit_histogram , range (len (digit_histogram ))):
66
+ sum += cnt * i ** digit_total
67
+
68
+ return n == sum
48
69
49
- expo = len (str (n )) # power, all number will be raised to
50
- # each digit will be multiplied expo times
51
- temp = [(int (i ) ** expo ) for i in str (n )]
52
70
53
- # check if sum of cube of each digit is equal to number
54
- return n == sum (temp )
71
+ def narcissistic_number (n : int ) -> bool :
72
+ """Return True if n is a narcissistic number or False if it is not.
73
+
74
+ >>> all(armstrong_number(n) for n in PASSING)
75
+ True
76
+ >>> any(armstrong_number(n) for n in FAILING)
77
+ False
78
+ """
79
+ if not isinstance (n , int ) or n < 1 :
80
+ return False
81
+ expo = len (str (n )) # the power that all digits will be raised to
82
+ # check if sum of each digit multiplied expo times is equal to number
83
+ return n == sum (int (i ) ** expo for i in str (n ))
55
84
56
85
57
86
def main ():
@@ -61,6 +90,7 @@ def main():
61
90
num = int (input ("Enter an integer to see if it is an Armstrong number: " ).strip ())
62
91
print (f"{ num } is { '' if armstrong_number (num ) else 'not ' } an Armstrong number." )
63
92
print (f"{ num } is { '' if narcissistic_number (num ) else 'not ' } an Armstrong number." )
93
+ print (f"{ num } is { '' if pluperfect_number (num ) else 'not ' } an Armstrong number." )
64
94
65
95
66
96
if __name__ == "__main__" :
0 commit comments