Posted on by Kalkicode
Code Number

# Check if a number is Triperfect Number

The Triperfect Number is a concept in number theory. A positive integer 'n' is said to be Triperfect if the sum of its proper divisors (excluding 'n' itself) is equal to three times the number 'n'. In other words, if the sum of the divisors of 'n' (excluding 'n') is equal to 3 times 'n', then 'n' is a Triperfect Number.

## Problem Statement

Given a positive integer 'number', we need to check whether it is a Triperfect Number or not.

## Explanation with Suitable Example

Let's take a few examples to understand the concept of Triperfect Numbers:

1. Consider 'number = 120'. The proper divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, which sum up to 360. Now, 3 times 'number' is 3 * 120 = 360. Since the sum of proper divisors is equal to 3 times 'number', i.e., 360 = 360, 'number = 120' is a Triperfect Number.

2. Consider 'number = 567'. The proper divisors of 567 are 1, 3, 7, 9, 21, 27, 63, 189, which sum up to 320. Now, 3 times 'number' is 3 * 567 = 1701. Since the sum of proper divisors is not equal to 3 times 'number', i.e., 320 ≠ 1701, 'number = 567' is not a Triperfect Number.

## Pseudocode

``````1. Input: 'number'
2. If 'number' is negative, return.
3. Initialize 'sum' as 'number + 1'.
4. Set 'n' to 2.
5. Repeat the following until 'n * n <= number':
a. If 'number' is divisible by 'n':
i. If 'n' is a factor of 'number' (i.e., 'number / n' equals 'n'), add 'n' to 'sum'.
ii. Otherwise, add both 'n' and 'number / n' to 'sum'.
b. Increment 'n' by 1.
6. If 'number * 3' is equal to 'sum', print '[number] Is Triperfect Number'.
Otherwise, print '[number] Is Not Triperfect Number'.
``````

## Algorithm

2. If 'number' is negative, return as Triperfect Numbers are defined for positive integers only.
3. Initialize 'sum' as 'number + 1'.
4. Set 'n' to 2.
5. Repeat the following steps until 'n * n <= number': a. Check if 'number' is divisible by 'n'. b. If it is divisible: i. Check if 'n' is a factor of 'number' (i.e., 'number / n' equals 'n'). ii. If 'n' is a factor, add 'n' to 'sum'. iii. If 'n' is not a factor, add both 'n' and 'number / n' to 'sum'. c. Increment 'n' by 1.
6. After the loop, check if 'number * 3' is equal to 'sum'.
7. If they are equal, print '[number] Is Triperfect Number'.
8. If they are not equal, print '[number] Is Not Triperfect Number'.

Here given code implementation process.

``````// C program
// Check if a number is Triperfect Number
#include <stdio.h>

//Check whether given number is Triperfect Number or not
void is_triperfect_no(int number)
{
if (number < 0)
{
return;
}
int n = 2;
int sum = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if (number / n == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (number / n);
}
}
n++;
}
if (number * 3 == sum)
{
printf("\n [%d] Is Triperfect Number", number);
}
else
{
printf("\n [%d] Is Not Triperfect Number", number);
}
}
int main()
{
//Test case
is_triperfect_no(120);
is_triperfect_no(567);
is_triperfect_no(672);
return 0;
}``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````/*
Java program
Check if a number is Triperfect Number
*/
class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
public void is_triperfect_no(int number)
{
if (number < 0)
{
return;
}
int n = 2;
int sum = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if (number / n == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (number / n);
}
}
n++;
}
if (number * 3 == sum)
{
System.out.print("\n [" + number + "] Is Triperfect Number");
}
else
{
System.out.print("\n [" + number + "] Is Not Triperfect Number");
}
}
public static void main(String[] args)
{
TriperfectNumber obj = new TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
}
}``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````//Include header file
#include <iostream>
using namespace std;

/*
C++ program
Check if a number is Triperfect Number
*/

class TriperfectNumber
{
public:
//Check whether given number is Triperfect Number or not
void is_triperfect_no(int number)
{
if (number < 0)
{
return;
}
int n = 2;
int sum = number + 1;
//Find other factors
while ((n *n) <= number)
{
if (number % n == 0)
{
if (number / n == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (number / n);
}
}
n++;
}
if (number *3 == sum)
{
cout << "\n [" << number << "] Is Triperfect Number";
}
else
{
cout << "\n [" << number << "] Is Not Triperfect Number";
}
}
};
int main()
{
TriperfectNumber obj = TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
return 0;
}``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````//Include namespace system
using System;
/*
C# program
Check if a number is Triperfect Number
*/
class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
public void is_triperfect_no(int number)
{
if (number < 0)
{
return;
}
int n = 2;
int sum = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if (number / n == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (number / n);
}
}
n++;
}
if (number * 3 == sum)
{
Console.Write("\n [" + number + "] Is Triperfect Number");
}
else
{
Console.Write("\n [" + number + "] Is Not Triperfect Number");
}
}
public static void Main(String[] args)
{
TriperfectNumber obj = new TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
}
}``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````<?php

/*
Php program
Check if a number is Triperfect Number
*/

class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
public	function is_triperfect_no(\$number)
{
if (\$number < 0)
{
return;
}
\$n = 2;
\$sum = \$number + 1;
//Find other factors
while ((\$n * \$n) <= \$number)
{
if (\$number % \$n == 0)
{
if (intval(\$number / \$n) == \$n)
{
\$sum = \$sum + \$n;
}
else
{
\$sum = \$sum + \$n + (intval(\$number / \$n));
}
}
\$n++;
}
if (\$number * 3 == \$sum)
{
echo "\n [". \$number ."] Is Triperfect Number";
}
else
{
echo "\n [". \$number ."] Is Not Triperfect Number";
}
}
}

function main()
{
\$obj = new TriperfectNumber();
//Test case
\$obj->is_triperfect_no(120);
\$obj->is_triperfect_no(567);
\$obj->is_triperfect_no(672);
}
main();``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````/*
Node Js program
Check if a number is Triperfect Number
*/
class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
is_triperfect_no(number)
{
if (number < 0)
{
return;
}
var n = 2;
var sum = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if (parseInt(number / n) == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (parseInt(number / n));
}
}
n++;
}
if (number * 3 == sum)
{
process.stdout.write("\n [" + number + "] Is Triperfect Number");
}
else
{
process.stdout.write("\n [" + number + "] Is Not Triperfect Number");
}
}
}

function main()
{
var obj = new TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
}
main();``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````#   Python 3 program
#   Check if a number is Triperfect Number

class TriperfectNumber :
# Check whether given number is Triperfect Number or not
def is_triperfect_no(self, number) :
if (number < 0) :
return

n = 2
# Add first and last factors
sum = number + 1
# Find other factors
while ((n * n) <= number) :
if (number % n == 0) :
if (int(number / n) == n) :
sum = sum + n
else :
sum = sum + n + (int(number / n))

n += 1

if (number * 3 == sum) :
print("\n [", number ,"] Is Triperfect Number", end = "")
else :
print("\n [", number ,"] Is Not Triperfect Number", end = "")

def main() :
obj = TriperfectNumber()
# Test case
obj.is_triperfect_no(120)
obj.is_triperfect_no(567)
obj.is_triperfect_no(672)

if __name__ == "__main__": main()``````

#### Output

`````` [ 120 ] Is Triperfect Number
[ 567 ] Is Not Triperfect Number
[ 672 ] Is Triperfect Number``````
``````#   Ruby program
#   Check if a number is Triperfect Number

class TriperfectNumber
# Check whether given number is Triperfect Number or not
def is_triperfect_no(number)
if (number < 0)
return
end

n = 2
# Add first and last factors
sum = number + 1
# Find other factors
while ((n * n) <= number)
if (number % n == 0)
if (number / n == n)
sum = sum + n
else
sum = sum + n + (number / n)
end

end

n += 1
end

if (number * 3 == sum)
print("\n [", number ,"] Is Triperfect Number")
else
print("\n [", number ,"] Is Not Triperfect Number")
end

end

end

def main()
obj = TriperfectNumber.new()
# Test case
obj.is_triperfect_no(120)
obj.is_triperfect_no(567)
obj.is_triperfect_no(672)
end

main()``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````/*
Scala program
Check if a number is Triperfect Number
*/
class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
def is_triperfect_no(number: Int): Unit = {
if (number < 0)
{
return;
}
var n: Int = 2;
var sum: Int = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if ((number / n).toInt == n)
{
sum = sum + n;
}
else
{
sum = sum + n + ((number / n).toInt);
}
}
n += 1;
}
if (number * 3 == sum)
{
print("\n [" + number + "] Is Triperfect Number");
}
else
{
print("\n [" + number + "] Is Not Triperfect Number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: TriperfectNumber = new TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
}
}``````

#### Output

`````` [120] Is Triperfect Number
[567] Is Not Triperfect Number
[672] Is Triperfect Number``````
``````/*
Swift 4 program
Check if a number is Triperfect Number
*/
class TriperfectNumber
{
//Check whether given number is Triperfect Number or not
func is_triperfect_no(_ number: Int)
{
if (number < 0)
{
return;
}
var n: Int = 2;
var sum: Int = number + 1;
//Find other factors
while ((n * n) <= number)
{
if (number % n == 0)
{
if (number / n == n)
{
sum = sum + n;
}
else
{
sum = sum + n + (number / n);
}
}
n += 1;
}
if (number * 3 == sum)
{
print("\n [", number ,"] Is Triperfect Number", terminator: "");
}
else
{
print("\n [", number ,"] Is Not Triperfect Number", terminator: "");
}
}
}
func main()
{
let obj: TriperfectNumber = TriperfectNumber();
//Test case
obj.is_triperfect_no(120);
obj.is_triperfect_no(567);
obj.is_triperfect_no(672);
}
main();``````

#### Output

`````` [ 120 ] Is Triperfect Number
[ 567 ] Is Not Triperfect Number
[ 672 ] Is Triperfect Number``````

## Time Complexity

The time complexity of the algorithm mainly depends on the loop that finds the divisors of the given number. The loop runs until 'n * n <= number', and each iteration takes constant time operations (checking divisibility, addition, etc.).

Hence, the time complexity of the algorithm is approximately O(sqrt(number)).

## Resultant Output Explanation

1. For the given number 120, the program correctly finds its divisors (1, 2, 3, ..., 60) and checks if it is a Triperfect Number. Since the sum of the divisors (360) is equal to 3 times the number (3 * 120 = 360), it correctly prints '[120] Is Triperfect Number'.

2. For the given number 567, the program correctly finds its divisors (1, 3, 7, ..., 189) and checks if it is a Triperfect Number. Since the sum of the divisors (320) is not equal to 3 times the number (3 * 567 = 1701), it correctly prints '[567] Is Not Triperfect Number'.

3. For the given number 672, the program correctly finds its divisors (1, 2, 3, ..., 336) and checks if it is a Triperfect Number. Since the sum of the divisors (1008) is equal to 3 times the number (3 * 672 = 2016), it correctly prints '[672] Is Triperfect Number'.

The program has provided the correct results for the given test cases.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post