Home > Enterprise >  Smart algorithm for finding perfect numbers
Smart algorithm for finding perfect numbers

Time:08-27

Is there an algorithm that is quicker than O(N^2) for finding perfect numbers from a sample 1:N? Or any general speed improvements to do less computation? I know we can remove odd numbers from the sample if we assume they are not perfect (unproven but we can assume it here regardless).

CodePudding user response:

Here is a way to do it (num is your number):

if sum(i for i in range (1, num) if num % i == 0) == num:
    print(num, "is a perfect number")
else:
    print(num, "is not a perfect number")
  • Related