Aspiring Number | Function to calculate sum of all proper divisors ; 1 is a proper divisor ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; Otherwise take both ; Calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; Checking condition for aspiring ; Driver Code
def getSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , int ( ( n ) ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if not n % i : NEW_LINE if n // i == i : NEW_LINE INDENT sum += i NEW_LINE DEDENT else : NEW_LINE INDENT sum += i NEW_LINE sum += ( n // i ) NEW_LINE DEDENT DEDENT return sum - n NEW_LINE DEDENT def getAliquot ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE s . add ( n ) NEW_LINE next = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = getSum ( n ) NEW_LINE if n not in s : NEW_LINE return n NEW_LINE s . add ( n ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT def isPerfect ( n ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( 2 , int ( ( n ** ( 1 / 2 ) ) ) + 1 ) : NEW_LINE INDENT if not n % i : NEW_LINE sum += ( i + n // i ) NEW_LINE DEDENT if sum == n and n != 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isAspiring ( n ) : NEW_LINE INDENT alq = getAliquot ( n ) NEW_LINE if ( isPerfect ( alq ) and not isPerfect ( n ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 25 NEW_LINE if ( isAspiring ( n ) ) : NEW_LINE INDENT print ( " Aspiring " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Aspiring " ) NEW_LINE DEDENT