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 ?> |
Javascript
<script> // Javascript 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. let sum = 1 + n; // Find all divisors and add them let i = 2; while (i * i <= n) { if (n % i == 0) { if (parseInt(n / i, 10) == i) sum = sum + i; else sum = sum + i + parseInt(n / i, 10); } 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 ; } let n = 120; if (isTriPerfect(n)) document.write(n + " is a Triperfect number" ); // This code is contributed by suresh07. </script> |
120 is a Triperfect number
Time Complexity: O(log n)
Auxiliary Space: O(1)
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!