Check if a number is Triperfect Number

Given a number N. The tasks is to check if the given number is a Triperfect Number.

Triperfect Number: A Number is a Triperfect Number if it is equal to three times the sum of its divisors, that is, 3 times sum of its positive divisors.

Examples:



Input: N = 15
Output: false
Divisors of 15 are 1, 3, 5 and 15. Sum of 
divisors is 24 which is not equal to 3*15 i.e. 45.

Input: N = 120
Output: true
Sum of divisors is 360 i.e. 3*120.
Hence 120 is a triperfect number.

A Simple Solution is to go through every number from 1 to N and check if it is a divisor. Maintain sum of all divisors. If sum becomes equal to 3*N, then return true, else return false.

An Efficient Solution is to go through numbers till square root of N. If a number ‘i’ divides n, then add both ‘i’ and n/i to sum.

Below is the implementation of the efficient approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to check if a given 
// number is Triperfect or not 
#include<bits/stdc++.h>
using namespace std;
  
// Returns true if n is Triperfect 
bool isTriPerfect(int n )
{
    // To store sum of divisors. 
    // Adding 1 and n since they are divisors of n.
    int sum = 1 + n;
      
    // Find all divisors and add them 
    int i = 2;
    while (i * i <= n)
    {
    if (n % i == 0)
        
            if (n / i == i)
                sum = sum + i;
            else
                sum = sum + i + n / i;
        }
        i += 1;
    }
      
    // If sum of divisors is equal to 
    // 3 * n, then n is a Triperfect number 
    if (sum == 3 * n and n != 1)
       return true;
    else
       false;
}
  
// Driver program 
int main()
{
 int n = 120;
  
 if (isTriPerfect(n)) 
    cout<<n<<" is a Triperfect number";    
}
  
//This code is contributed by 
// Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to check if a given 
// number is Triperfect or not 
  
public class GFG{
      
    // Returns true if n is Triperfect 
    static boolean isTriPerfect(int n ) 
    
        // To store sum of divisors. 
        // Adding 1 and n since they are divisors of n. 
        int sum = 1 + n; 
          
        // Find all divisors and add them 
        int i = 2
        while (i * i <= n) 
        
        if (n % i == 0
            
                if (n / i == i) 
                    sum = sum + i; 
                else
                    sum = sum + i + n / i; 
            
            i += 1
        
          
        // If sum of divisors is equal to 
        // 3 * n, then n is a Triperfect number 
        if (sum == 3 * n & n != 1
            return true
        else
            return  false
    
      
    // Driver program 
    public static void main(String []args)
    
    int n = 120
      
    if (isTriPerfect(n)) 
        System.out.println(n + " is a Triperfect number");     
    
      
    //This code is contributed by 
    // Ryuga
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if a given 
# number is Triperfect or not 
  
# Returns true if n is Triperfect 
def isTriPerfect( n ): 
      
    # To store sum of divisors. 
    # Adding 1 and n since they are divisors of n.
    sum = 1 + n
      
    # Find all divisors and add them 
    i = 2
    while i * i <= n: 
        if n % i == 0
            if n / i == i:
                sum = sum + i
            else:
                sum = sum + i + n /
        i += 1
      
    # If sum of divisors is equal to 
    # 3 * n, then n is a Triperfect number 
    return (True if sum == 3 * n and n != 1 else False
  
# Driver program 
n = 120
  
if isTriPerfect (n): 
    print(n, "is a Triperfect number"
            

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if a given 
// number is Triperfect or not 
using System;
public class GFG{
      
    // Returns true if n is Triperfect 
    static bool isTriPerfect(int n ) 
    
        // To store sum of divisors. 
        // Adding 1 and n since they are divisors of n. 
        int sum = 1 + n; 
          
        // Find all divisors and add them 
        int i = 2; 
        while (i * i <= n) 
        
        if (n % i == 0) 
            
                if (n / i == i) 
                    sum = sum + i; 
                else
                    sum = sum + i + n / i; 
            
            i += 1; 
        
          
        // If sum of divisors is equal to 
        // 3 * n, then n is a Triperfect number 
        if (sum == 3 * n & n != 1) 
            return true
        else
            return false
    
      
    // Driver program 
    public static void Main()
    
    int n = 120; 
      
    if (isTriPerfect(n)) 
        Console.WriteLine(n + " is a Triperfect number");     
    
      
    //This code is contributed by 
    // Mukul Singh
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?PHP
// PHP code to check if a given 
// number is Triperfect or not 
  
// Returns true if n is Triperfect 
function isTriPerfect($n )
{
    // To store sum of divisors. 
    // Adding 1 and n since they 
    // are divisors of n.
    $sum = 1 + $n;
      
    // Find all divisors and add them 
    $i = 2;
    while ($i * $i <= $n)
    {
        if ($n % $i == 0)
        
            if ($n / $i == $i)
                $sum = $sum + $i;
            else
                $sum = $sum + $i + $n / $i;
        }
        $i += 1;
    }
      
    // If sum of divisors is equal to 
    // 3 * n, then n is a Triperfect number 
    if ($sum == 3 * $n and $n != 1)
        return true;
    else
        false;
}
  
// Driver Code 
$n = 120;
  
if (isTriPerfect($n)) 
    echo $n . " is a Triperfect number";
else
    echo $n . " is not a Triperfect number";
  
// This code is contributed by mits
?>

chevron_right


Output:

120 is a Triperfect number

Some interesting facts about TriPerfect Numbers:

  • Till now only 6 Triperfect numbers are known. These are 120, 672, 523776, 459818240, 1476304896 and 51001180160.
  • It has not been proven that more Triperfect Numbers don’t exist but it is believed that these are the only six.

Similarly, we can check for quad-perfect numbers(sum of factors = 4*n), five-perfect numbers( sum of factors = 5*n) and so on. There are currently 36 quad-perfect numbers known and 65 five-perfect numbers known.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


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