Hint for straightforward solution


  • 0
    M

    All perfect numbers should have the form (2^0 + 2^1 + 2^2 + ... + 2^n) * 2^n.

    For example, let n = 6,

     127 * 64
    = 127 * ( 1 + 1 + 2 + 4 + 8 + 16 + 32)
    = 127 + 127 * (1 + 2 + 4 + 8 + 16 + 32)
    = (1 + 2 + 4 + 8 + 16 + 32 + 64) + 127 * (1 + 2 + 4 + 8 + 16 + 32)
    = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 * 1 + 127 * 2 + 127 * 4 + 127 * 8 + 127 * 16 + 127 * 32
    

    The numbers are exactly all positive divisors.

    In python

    perfect = (1<<n-1) * ((1<<n)- 1)
    

    Just to make sure ((1<<n)-1) is a prime number.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.