# Aspiring Number

Given a number n, We need to check whether n is aspiring number or not. The number n is called an aspiring number if its aliquot sequence< terminates in a perfect number, and it is not a perfect number itself. First few aspiring numbers are : 25, 95, 119, 143, 417, 445, 565, 608, 650, 652….

Examples :

```Input : 25
Output : Yes.
Explanation : Terminating number of
aliquot sequence of 25 is 6 which is
perfect number.

Input :  24
Output : No.
Explanation : Terminating number of
aliquot sequence of 24 is 0 which is
not a perfect number.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : First we find the terminating number of aliquot sequence of given input and then check if it is perfect number or not (as per definition). Given below is implementation for checking aspiring number.

## C++

 `// C++ implementation to check whether ` `// a number is aspiring or not ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate sum of all proper ` `// divisors ` `int` `getSum(``int` `n) ` `{ ` `    ``int` `sum = 0; ``// 1 is a proper divisor ` ` `  `    ``// Note that this loop runs till square root ` `    ``// of n ` `    ``for` `(``int` `i = 1; i <= ``sqrt``(n); i++) { ` `        ``if` `(n % i == 0) { ` ` `  `            ``// If divisors are equal, take only one ` `            ``// of them ` `            ``if` `(n / i == i) ` `                ``sum = sum + i; ` ` `  `            ``else` `// Otherwise take both ` `            ``{ ` `                ``sum = sum + i; ` `                ``sum = sum + (n / i); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// calculate sum of all proper divisors only ` `    ``return` `sum - n; ` `} ` ` `  `// Function to get last number of Aliquot Sequence. ` `int` `getAliquot(``int` `n) ` `{ ` `    ``unordered_set<``int``> s; ` `    ``s.insert(n); ` ` `  `    ``int` `next = 0; ` `    ``while` `(n > 0) { ` ` `  `        ``// Calculate next term from previous term ` `        ``n = getSum(n); ` ` `  `        ``if` `(s.find(n) != s.end())  ` `            ``return` `n;         ` ` `  `        ``s.insert(n); ` `    ``} ` `    ``return` `0; ` `} ` ` `  `// Returns true if n is perfect ` `bool` `isPerfect(``int` `n) ` `{ ` `    ``// To store sum of divisors ` `    ``long` `long` `int` `sum = 1; ` ` `  `    ``// Find all divisors and add them ` `    ``for` `(``long` `long` `int` `i = 2; i * i <= n; i++) ` `        ``if` `(n % i == 0) ` `            ``sum = sum + i + n / i; ` ` `  `    ``// If sum of divisors is equal to ` `    ``// n, then n is a perfect number ` `    ``if` `(sum == n && n != 1) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// Returns true if n is aspiring ` `// else returns false ` `bool` `isAspiring(``int` `n) ` `{ ` `    ``// checking codition for aspiring ` `    ``int` `alq = getAliquot(n); ` `    ``if` `(isPerfect(alq) && !isPerfect(n)) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `n = 25; ` `    ``if` `(isAspiring(n)) ` `        ``cout << ``"Aspiring"` `<< endl; ` `    ``else` `        ``cout << ``"Not Aspiring"` `<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to check whether  ` `// a number is aspiring or not ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to calculate sum of ` `    ``//  all proper divisors  ` `    ``static` `int` `getSum(``int` `n) ` `    ``{ ` `        ``int` `sum = ``0``; ``// 1 is a proper divisor  ` ` `  `        ``// Note that this loop runs till   ` `        ``// square root of n  ` `        ``for` `(``int` `i = ``1``; i <= Math.sqrt(n); i++)  ` `        ``{ ` `            ``if` `(n % i == ``0``) ` `            ``{ ` ` `  `                ``// If divisors are equal,  ` `                ``// take only one of them  ` `                ``if` `(n / i == i) ` `                ``{ ` `                    ``sum = sum + i; ` `                ``}  ` `                ``else` `// Otherwise take both  ` `                ``{ ` `                    ``sum = sum + i; ` `                    ``sum = sum + (n / i); ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// calculate sum of all ` `        ``// proper divisors only  ` `        ``return` `sum - n; ` `    ``} ` ` `  `    ``// Function to get last number  ` `    ``// of Aliquot Sequence.  ` `    ``static` `int` `getAliquot(``int` `n) ` `    ``{ ` `        ``TreeSet s = ``new` `TreeSet(); ` `        ``s.add(n); ` ` `  `        ``int` `next = ``0``; ` `        ``while` `(n > ``0``) ` `        ``{ ` ` `  `            ``// Calculate next term from previous term  ` `            ``n = getSum(n); ` ` `  `            ``if` `(s.contains(n) & n != s.last()) ` `            ``{ ` `                ``return` `n; ` `            ``} ` ` `  `            ``s.add(n); ` `        ``} ` `        ``return` `0``; ` `    ``} ` ` `  `    ``// Returns true if n is perfect  ` `    ``static` `boolean` `isPerfect(``int` `n) ` `    ``{ ` `        ``// To store sum of divisors  ` `        ``int` `sum = ``1``; ` ` `  `        ``// Find all divisors and add them  ` `        ``for` `(``int` `i = ``2``; i * i <= n; i++)  ` `        ``{ ` `            ``if` `(n % i == ``0``)  ` `            ``{ ` `                ``sum = sum + i + n / i; ` `            ``} ` `        ``} ` ` `  `        ``// If sum of divisors is equal to  ` `        ``// n, then n is a perfect number  ` `        ``if` `(sum == n && n != ``1``)  ` `        ``{ ` `            ``return` `true``; ` `        ``} ` ` `  `        ``return` `false``; ` `    ``} ` ` `  `    ``// Returns true if n is aspiring  ` `    ``// else returns false  ` `    ``static` `boolean` `isAspiring(``int` `n) ` `    ``{ ` `         `  `        ``// checking codition for aspiring  ` `        ``int` `alq = getAliquot(n); ` `        ``if` `(isPerfect(alq) && !isPerfect(n))  ` `        ``{ ` `            ``return` `true``; ` `        ``}  ` `        ``else` `        ``{ ` `            ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``25``; ` `        ``if` `(isAspiring(n))  ` `        ``{ ` `            ``System.out.println(``"Aspiring"``); ` `        ``}  ` `        ``else`  `        ``{ ` `            ``System.out.println(``"Not Aspiring"``); ` `        ``} ` `    ``} ` `} ` ` `  `/* This code has been contributed  ` `by PrinciRaj1992*/`

Output:

```Aspiring
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : princiraj1992, gfg_sal_gfg

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.