|
1 |
| -""" |
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 |
| -
|
5 |
| -For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. |
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 |
10 |
| -""" |
11 |
| -PASSING = (1, 153, 370, 371, 1634, 24678051, 115132219018763992565095597973971522401) |
12 |
| -FAILING: tuple = (-153, -1, 0, 1.2, 200, "A", [], {}, None) |
13 |
| - |
14 |
| - |
15 |
| -def armstrong_number(n: int) -> bool: |
16 |
| - """ |
17 |
| - Return True if n is an Armstrong number or False if it is not. |
18 |
| -
|
19 |
| - >>> all(armstrong_number(n) for n in PASSING) |
20 |
| - True |
21 |
| - >>> any(armstrong_number(n) for n in FAILING) |
22 |
| - False |
23 |
| - """ |
24 |
| - if not isinstance(n, int) or n < 1: |
25 |
| - return False |
26 |
| - |
27 |
| - # Initialization of sum and number of digits. |
28 |
| - total = 0 |
29 |
| - number_of_digits = 0 |
30 |
| - temp = n |
31 |
| - # Calculation of digits of the number |
32 |
| - number_of_digits = len(str(n)) |
33 |
| - # Dividing number into separate digits and find Armstrong number |
34 |
| - temp = n |
35 |
| - while temp > 0: |
36 |
| - rem = temp % 10 |
37 |
| - total += rem**number_of_digits |
38 |
| - temp //= 10 |
39 |
| - return n == total |
40 |
| - |
41 |
| - |
42 |
| -def pluperfect_number(n: int) -> bool: |
43 |
| - """Return True if n is a pluperfect number or False if it is not |
44 |
| -
|
45 |
| - >>> all(armstrong_number(n) for n in PASSING) |
46 |
| - True |
47 |
| - >>> any(armstrong_number(n) for n in FAILING) |
48 |
| - False |
49 |
| - """ |
50 |
| - if not isinstance(n, int) or n < 1: |
51 |
| - return False |
52 |
| - |
53 |
| - # Init a "histogram" of the digits |
54 |
| - digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
55 |
| - digit_total = 0 |
56 |
| - total = 0 |
57 |
| - temp = n |
58 |
| - while temp > 0: |
59 |
| - temp, rem = divmod(temp, 10) |
60 |
| - digit_histogram[rem] += 1 |
61 |
| - digit_total += 1 |
62 |
| - |
63 |
| - for cnt, i in zip(digit_histogram, range(len(digit_histogram))): |
64 |
| - total += cnt * i**digit_total |
65 |
| - |
66 |
| - return n == total |
67 |
| - |
68 |
| - |
69 |
| -def narcissistic_number(n: int) -> bool: |
70 |
| - """Return True if n is a narcissistic number or False if it is not. |
71 |
| -
|
72 |
| - >>> all(armstrong_number(n) for n in PASSING) |
73 |
| - True |
74 |
| - >>> any(armstrong_number(n) for n in FAILING) |
75 |
| - False |
76 |
| - """ |
77 |
| - if not isinstance(n, int) or n < 1: |
78 |
| - return False |
79 |
| - expo = len(str(n)) # the power that all digits will be raised to |
80 |
| - # check if sum of each digit multiplied expo times is equal to number |
81 |
| - return n == sum(int(i) ** expo for i in str(n)) |
82 |
| - |
83 |
| - |
84 |
| -def main(): |
85 |
| - """ |
86 |
| - Request that user input an integer and tell them if it is Armstrong number. |
87 |
| - """ |
88 |
| - num = int(input("Enter an integer to see if it is an Armstrong number: ").strip()) |
89 |
| - print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.") |
90 |
| - print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.") |
91 |
| - print(f"{num} is {'' if pluperfect_number(num) else 'not '}an Armstrong number.") |
92 |
| - |
93 |
| - |
94 |
| -if __name__ == "__main__": |
95 |
| - import doctest |
96 |
| - |
97 |
| - doctest.testmod() |
98 |
| - main() |
| 1 | +""" |
| 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 | +
|
| 5 | +For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. |
| 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 |
| 10 | +""" |
| 11 | +PASSING = (1, 153, 370, 371, 1634, 24678051, 115132219018763992565095597973971522401) |
| 12 | +FAILING: tuple = (-153, -1, 0, 1.2, 200, "A", [], {}, None) |
| 13 | + |
| 14 | + |
| 15 | +def armstrong_number(n: int) -> bool: |
| 16 | + """ |
| 17 | + Return True if n is an Armstrong number or False if it is not. |
| 18 | +
|
| 19 | + >>> all(armstrong_number(n) for n in PASSING) |
| 20 | + True |
| 21 | + >>> any(armstrong_number(n) for n in FAILING) |
| 22 | + False |
| 23 | + """ |
| 24 | + if not isinstance(n, int) or n < 1: |
| 25 | + return False |
| 26 | + |
| 27 | + # Initialization of sum and number of digits. |
| 28 | + total = 0 |
| 29 | + number_of_digits = 0 |
| 30 | + temp = n |
| 31 | + # Calculation of digits of the number |
| 32 | + number_of_digits = len(str(n)) |
| 33 | + # Dividing number into separate digits and find Armstrong number |
| 34 | + temp = n |
| 35 | + while temp > 0: |
| 36 | + rem = temp % 10 |
| 37 | + total += rem**number_of_digits |
| 38 | + temp //= 10 |
| 39 | + return n == total |
| 40 | + |
| 41 | + |
| 42 | +def pluperfect_number(n: int) -> bool: |
| 43 | + """Return True if n is a pluperfect number or False if it is not |
| 44 | +
|
| 45 | + >>> all(armstrong_number(n) for n in PASSING) |
| 46 | + True |
| 47 | + >>> any(armstrong_number(n) for n in FAILING) |
| 48 | + False |
| 49 | + """ |
| 50 | + if not isinstance(n, int) or n < 1: |
| 51 | + return False |
| 52 | + |
| 53 | + # Init a "histogram" of the digits |
| 54 | + digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 55 | + digit_total = 0 |
| 56 | + total = 0 |
| 57 | + temp = n |
| 58 | + while temp > 0: |
| 59 | + temp, rem = divmod(temp, 10) |
| 60 | + digit_histogram[rem] += 1 |
| 61 | + digit_total += 1 |
| 62 | + |
| 63 | + for cnt, i in zip(digit_histogram, range(len(digit_histogram))): |
| 64 | + total += cnt * i**digit_total |
| 65 | + |
| 66 | + return n == total |
| 67 | + |
| 68 | + |
| 69 | +def narcissistic_number(n: int) -> bool: |
| 70 | + """Return True if n is a narcissistic number or False if it is not. |
| 71 | +
|
| 72 | + >>> all(armstrong_number(n) for n in PASSING) |
| 73 | + True |
| 74 | + >>> any(armstrong_number(n) for n in FAILING) |
| 75 | + False |
| 76 | + """ |
| 77 | + if not isinstance(n, int) or n < 1: |
| 78 | + return False |
| 79 | + expo = len(str(n)) # the power that all digits will be raised to |
| 80 | + # check if sum of each digit multiplied expo times is equal to number |
| 81 | + return n == sum(int(i) ** expo for i in str(n)) |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + """ |
| 86 | + Request that user input an integer and tell them if it is Armstrong number. |
| 87 | + """ |
| 88 | + num = int(input("Enter an integer to see if it is an Armstrong number: ").strip()) |
| 89 | + print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.") |
| 90 | + print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.") |
| 91 | + print(f"{num} is {'' if pluperfect_number(num) else 'not '}an Armstrong number.") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + import doctest |
| 96 | + |
| 97 | + doctest.testmod() |
| 98 | + main() |
0 commit comments