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

Function is_perfect_number

maths/primelib.py:688–722  ·  view source on GitHub ↗

input: positive integer 'number' > 1 returns true if 'number' is a perfect number otherwise false. >>> is_perfect_number(28) True >>> is_perfect_number(824) False >>> is_perfect_number(-1) Traceback (most recent call last): ... AssertionError: 'number' m

(number)

Source from the content-addressed store, hash-verified

686
687
688def is_perfect_number(number):
689 """
690 input: positive integer 'number' > 1
691 returns true if 'number' is a perfect number otherwise false.
692
693 >>> is_perfect_number(28)
694 True
695 >>> is_perfect_number(824)
696 False
697 >>> is_perfect_number(-1)
698 Traceback (most recent call last):
699 ...
700 AssertionError: 'number' must been an int and >= 1
701 >>> is_perfect_number("test")
702 Traceback (most recent call last):
703 ...
704 AssertionError: 'number' must been an int and >= 1
705 """
706
707 # precondition
708 assert isinstance(number, int) and (number > 1), (
709 "'number' must been an int and >= 1"
710 )
711
712 divisors = get_divisors(number)
713
714 # precondition
715 assert (
716 isinstance(divisors, list)
717 and (divisors[0] == 1)
718 and (divisors[len(divisors) - 1] == number)
719 ), "Error in help-function getDivisiors(...)"
720
721 # summed all divisors up to 'number' (exclusive), hence [:-1]
722 return sum(divisors[:-1]) == number
723
724
725# ------------------------------------------------------------

Callers

nothing calls this directly

Calls 1

get_divisorsFunction · 0.85

Tested by

no test coverage detected