Check if a number is Triperfect Number
Given a number
. 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++
// 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 |
Java
// 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 } |
Python3
# 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 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") |
C#
// 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 } |
PHP
<?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 ?> |
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.
Recommended Posts:
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Check if a number is multiple of 5 without using / and % operators
- Check whether a given point lies inside a triangle or not
- Given a number, find the next smallest palindrome
- Check divisibility by 7
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number
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.



