Aspiring Number
Last Updated :
06 Apr, 2023
Given a number n, We need to check whether n is an 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.
Approach: First we find the terminating number of the aliquot sequence of the given input and then check if it is a perfect number or not (as per definition). Given below is the implementation for checking an aspiring number.
C++
#include <bits/stdc++.h>
using namespace std;
int getSum( int n)
{
int sum = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i)
sum = sum + i;
else
{
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;
}
|
Java
import java.util.*;
class GFG
{
static int getSum( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
{
sum = sum + i;
}
else
{
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" );
}
}
}
|
Python3
def getSum(n):
sum = 0
for i in range ( 1 , int ((n) * * ( 1 / 2 )) + 1 ):
if not n % i:
if n / / i = = i:
sum + = i
else :
sum + = i
sum + = (n / / i)
return sum - n
def getAliquot(n):
s = set ()
s.add(n)
next = 0
while (n > 0 ):
n = getSum(n)
if n not in s:
return n
s.add(n)
return 0
def isPerfect(n):
sum = 1
for i in range ( 2 , int ((n * * ( 1 / 2 ))) + 1 ):
if not n % i:
sum + = (i + n / / i)
if sum = = n and n ! = 1 :
return True
return False
def isAspiring(n):
alq = getAliquot(n)
if (isPerfect(alq) and not isPerfect(n)):
return True
else :
return False
n = 25
if (isAspiring(n)):
print ( "Aspiring" )
else :
print ( "Not Aspiring" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getSum( int n)
{
int sum = 0;
for ( int i = 1; i <= ( int )Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
sum = sum + i;
}
else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum - n;
}
static int getAliquot( int n)
{
HashSet< int > s = new HashSet< int >();
s.Add(n);
while (n > 0)
{
n = getSum(n);
if (s.Contains(n))
{
return n;
}
s.Add(n);
}
return 0;
}
static bool 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 bool 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))
{
Console.WriteLine( "Aspiring" );
}
else
{
Console.WriteLine( "Not Aspiring" );
}
}
}
|
Javascript
<script>
function getSum(n) {
var sum = 0;
for ( var i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
sum = sum + i;
} else
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum - n;
}
function getAliquot(n) {
var s = new Set();
s.add(n);
var next = 0;
while (n > 0) {
n = getSum(n);
if (s.has(n) & n != s[s.length-1]) {
return n;
}
s.add(n);
}
return 0;
}
function isPerfect(n) {
var sum = 1;
for ( var i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum = sum + i + n / i;
}
}
if (sum == n && n != 1) {
return true ;
}
return false ;
}
function isAspiring(n) {
var alq = getAliquot(n);
if (isPerfect(alq) && !isPerfect(n)) {
return true ;
} else {
return false ;
}
}
var n = 25;
if (isAspiring(n)) {
document.write( "Aspiring" );
} else {
document.write( "Not Aspiring" );
}
</script>
|
Time Complexity: O(n)
Auxilitary Space Complexity : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...