MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / armstrong_number

Function armstrong_number

maths/special_numbers/armstrong_numbers.py:16–40  ·  view source on GitHub ↗

Return True if n is an Armstrong number or False if it is not. >>> all(armstrong_number(n) for n in PASSING) True >>> any(armstrong_number(n) for n in FAILING) False

(n: int)

Source from the content-addressed store, hash-verified

14
15
16def armstrong_number(n: int) -> bool:
17 """
18 Return True if n is an Armstrong number or False if it is not.
19
20 >>> all(armstrong_number(n) for n in PASSING)
21 True
22 >>> any(armstrong_number(n) for n in FAILING)
23 False
24 """
25 if not isinstance(n, int) or n < 1:
26 return False
27
28 # Initialization of sum and number of digits.
29 total = 0
30 number_of_digits = 0
31 temp = n
32 # Calculation of digits of the number
33 number_of_digits = len(str(n))
34 # Dividing number into separate digits and find Armstrong number
35 temp = n
36 while temp > 0:
37 rem = temp % 10
38 total += rem**number_of_digits
39 temp //= 10
40 return n == total
41
42
43def pluperfect_number(n: int) -> bool:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected