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

Function narcissistic_number

maths/special_numbers/armstrong_numbers.py:70–82  ·  view source on GitHub ↗

Return True if n is a narcissistic number or False if it is not. >>> all(narcissistic_number(n) for n in PASSING) True >>> any(narcissistic_number(n) for n in FAILING) False

(n: int)

Source from the content-addressed store, hash-verified

68
69
70def narcissistic_number(n: int) -> bool:
71 """Return True if n is a narcissistic number or False if it is not.
72
73 >>> all(narcissistic_number(n) for n in PASSING)
74 True
75 >>> any(narcissistic_number(n) for n in FAILING)
76 False
77 """
78 if not isinstance(n, int) or n < 1:
79 return False
80 expo = len(str(n)) # the power that all digits will be raised to
81 # check if sum of each digit multiplied expo times is equal to number
82 return n == sum(int(i) ** expo for i in str(n))
83
84
85def main():

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected