给定一个数字 。任务是检查给定的号码是否是Triperfect号码。
Triperfect Number :如果数字等于其除数之和的三倍,即其正除数之和的3倍,则数字为Triperfect数。
例子:
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.
一个简单的解决方案是遍历从1到N的每个数字,并检查它是否为除数。保持所有除数之和。如果总和等于3 * N,则返回true,否则返回false。
一个有效的解决方案是遍历数字直到N的平方根。如果数字“ i”除以n,则将“ i”和n / i相加。
下面是有效方法的实现:
C++
// CPP code to check if a given
// number is Triperfect or not
#include
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<
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
输出:
120 is a Triperfect number
有关TriPerfect Numbers的一些有趣事实:
- 到目前为止,只有6个Triperfect号码是已知的。它们是120、672、523776、459818240、1476304896和51001180160。
- 尚未证明不存在更多的Triperfect数,但据信这是仅有的六个。
同样,我们可以检查四次完美数(因数之和= 4 * n),五次完美数(因数之和= 5 * n)等等。当前,已知36个四全数和65个五全数。