Aspiring Number | C ++ implementation to check whether a number is aspiring or not ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else 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 program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } int getAliquot ( int n ) { unordered_set < int > s ; s . insert ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . find ( n ) != s . end ( ) ) return n ; s . insert ( n ) ; } return 0 ; } bool isPerfect ( int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + n / i ; if ( sum == n && n != 1 ) return true ; return false ; } bool isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) return true ; else return false ; } int main ( ) { int n = 25 ; if ( isAspiring ( n ) ) cout << " Aspiring " << endl ; else cout << " Not ▁ Aspiring " << endl ; return 0 ; }