We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. 

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.


Example:

Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14



Note:
The input number n will not exceed 100,000,000. (1e8)

Source: leetcode | Difficulty: EASY
Tags: Mathematics

Starter code: class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        

Solutions:
class Solution:

	def checkPerfectNumber(self, num):
		perfect = set([6, 28, 496, 8128, 33550336, 8589869056])
		return num in perfect


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 0:
			return False
		p = 1
		n = 1
		while n < num:
			n = 2 ** (p - 1) * (2 ** p - 1)
			if n == num:
				return True
			p += 1
		return False


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 1:
			return False
		ssum = 1
		i = 2
		while i ** 2 < num:
			if not num % i:
				ssum += i
				ssum += num / i
			i += 1
		return ssum == num


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 1:
			return False
		factors = []
		for i in range(1, int(num ** 0.5) + 1):
			if num % i == 0:
				factors.append(i)
		for factor in factors:
			if num / factor not in factors and factor > 1:
				factors.append(num / factor)
		if num == sum((i for i in factors)):
			return True
		else:
			return False


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 5:
			return False
		sum = 1
		i = 2
		while i <= int(num ** 0.5):
			if num % i == 0:
				sum += i
				if i != num // i:
					sum += num // i
			i += 1
		return num == sum


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 1:
			return False
		tot = 1
		for i in range(2, int(num ** 0.5) + 1):
			if num % i == 0:
				tot += i + num / i
				if tot > num:
					return False
		return tot == num


class Solution:

	def checkPerfectNumber(self, num):
		if num < 2:
			return False
		sum_ = 1
		for i in range(2, math.ceil(math.sqrt(num))):
			if num % i == 0:
				sum_ += i + num // i
		return sum_ == num


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 0:
			return False
		sum_of_divisors = 0
		n = 1
		while True:
			if n * n > num:
				break
			if num % n == 0:
				sum_of_divisors += n + num / n
			n += 1
		if (n - 1) * (n - 1) == num:
			sum_of_divisors -= n - 1
		return num == sum_of_divisors - num


class Solution:

	def checkPerfectNumber(self, num):
		if num <= 5:
			return False
		i = 2
		my_set = set({1})
		while i not in my_set and i < num // i:
			if num % i == 0:
				my_set.update([i, num // i])
			i += 1
		return sum(list(my_set)) == num


class Solution:

	def checkPerfectNumber(self, num):
		myset = set()
		result = 0
		if num == 1:
			return False
		i = 2
		while i < int(num / i):
			if num % i == 0:
				result += i
				result += int(num / i)
			i += 1
		return result + 1 == num