Tcefrep编号为N的数字,使得reverse(n)= N的适当除数之和。
6, 498906, 20671542, 41673714….
检查N是否为Tcefrep号码
给定数字N ,任务是检查N是否为Tcefrep数字。如果N是Tcefrep号码,则打印“是”,否则打印“否” 。
例子:
Input: N = 498906
Output: Yes
Explanation:
proper divisors of 498906 are 1, 2, 3, 6, 9, 18, 27, 54,
9239, 18478, 27717, 55434, 83151, 166302, 249453,
which sum to 609894, the reverse of 498906
Input: N = 120
Output: No
方法:
- 我们将找到N的适当除数之和
- 我们会发现N的反面
- 然后,我们将检查是否N个适当除数的总和等于逆转的N或不是,如果相等,则打印“是”其他打印“否”。
下面是上述方法的实现:
C++
// C++ implementation to check if N
// is a Tcefrep number
#include
using namespace std;
// Iterative function to
// reverse digits of num
int reverse(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
// Function to calculate sum of
// all proper divisors
// num --> given natural number
int properDivSum(int num)
{
// Final result of summation of divisors
int result = 0;
// find all divisors which divides 'num'
for (int i=2; i<=sqrt(num); i++)
{
// if 'i' is divisor of 'num'
if (num%i==0)
{
// if both divisors are same then add
// it only once else add both
if (i==(num/i))
result += i;
else
result += (i + num/i);
}
}
// Add 1 to the result as 1 is also a divisor
return (result + 1);
}
bool isTcefrep(int n)
{
return properDivSum(n) == reverse(n);
}
// Driver Code
int main()
{
// Given Number N
int N = 6;
// Function Call
if (isTcefrep(N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for above approach
class GFG{
// Iterative function to
// reverse digits of num
static int reverse(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to calculate sum of
// all proper divisors
// num --> given natural number
static int properDivSum(int num)
{
// Final result of summation of divisors
int result = 0;
// find all divisors which divides 'num'
for (int i = 2; i<= Math.sqrt(num); i++)
{
// if 'i' is divisor of 'num'
if (num % i == 0)
{
// if both divisors are same then add
// it only once else add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as 1
// is also a divisor
return (result + 1);
}
static boolean isTcefrep(int n)
{
return properDivSum(n) == reverse(n);
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
// Function Call
if (isTcefrep(N))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 implementation to check if N
# is a Tcefrep number
import math
# Iterative function to
# reverse digits of num
def reverse(num):
rev_num = 0
while(num > 0):
rev_num = rev_num * 10 + num % 10
num = num // 10
return rev_num
# Function to calculate sum of
# all proper divisors
# num --> given natural number
def properDivSum(num):
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
for i in range(2, (int)(math.sqrt(num)) + 1):
# if 'i' is divisor of 'num'
if (num % i == 0):
# if both divisors are same then add
# it only once else add both
if (i == (num // i)):
result += i
else:
result += (i + num / i)
# Add 1 to the result as 1 is also a divisor
return (result + 1)
def isTcefrep(n):
return properDivSum(n) == reverse(n);
# Driver Code
# Given Number N
N = 6
# Function Call
if(isTcefrep(N)):
print("Yes")
else:
print("No")
# This code is contributed by Sanjit Prasad
C#
// C# program for above approach
using System;
class GFG{
// Iterative function to
// reverse digits of num
static int reverse(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to calculate sum of
// all proper divisors
// num --> given natural number
static int properDivSum(int num)
{
// Final result of summation of divisors
int result = 0;
// find all divisors which divides 'num'
for (int i = 2; i<= Math.Sqrt(num); i++)
{
// if 'i' is divisor of 'num'
if (num % i == 0)
{
// if both divisors are same then add
// it only once else add both
if (i == (num / i))
result += i;
else
result += (i + num / i);
}
}
// Add 1 to the result as 1
// is also a divisor
return (result + 1);
}
static bool isTcefrep(int n)
{
return properDivSum(n) == reverse(n);
}
// Driver Code
public static void Main()
{
int N = 6;
// Function Call
if (isTcefrep(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Nidhi_Biet
Javascript
输出:
Yes
时间复杂度: O(n ^ 2)
参考:http://www.numbersaplenty.com/set/tcefrep_number/