Aspiring Number | Java 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 code
import java . util . * ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static int getAliquot ( int n ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; s . add ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . contains ( n ) & n != s . last ( ) ) { return n ; } s . add ( n ) ; } return 0 ; } static boolean isPerfect ( int n ) { int sum = 1 ; for ( 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 ; } static boolean isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 25 ; if ( isAspiring ( n ) ) { System . out . println ( " Aspiring " ) ; } else { System . out . println ( " Not ▁ Aspiring " ) ; } } }