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

Function pluperfect_number

maths/special_numbers/armstrong_numbers.py:43–67  ·  view source on GitHub ↗

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

(n: int)

Source from the content-addressed store, hash-verified

41
42
43def pluperfect_number(n: int) -> bool:
44 """Return True if n is a pluperfect number or False if it is not
45
46 >>> all(pluperfect_number(n) for n in PASSING)
47 True
48 >>> any(pluperfect_number(n) for n in FAILING)
49 False
50 """
51 if not isinstance(n, int) or n < 1:
52 return False
53
54 # Init a "histogram" of the digits
55 digit_histogram = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
56 digit_total = 0
57 total = 0
58 temp = n
59 while temp > 0:
60 temp, rem = divmod(temp, 10)
61 digit_histogram[rem] += 1
62 digit_total += 1
63
64 for cnt, i in zip(digit_histogram, range(len(digit_histogram))):
65 total += cnt * i**digit_total
66
67 return n == total
68
69
70def narcissistic_number(n: int) -> bool:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected