How To Check Whether a Number is Tcefrep Number or Not in Java?
A number is said to be a Tcefrep number, if the reverse of that number is equal to the sum of the proper divisors of the original number.
For more clarification, we find the sum of the divisors of a number and find the reverse of the original number. If both the sum value and the reverse of the given number are the same then we can say that the given number is a Tcefrep number.
Some examples of Tcefrep numbers are: 6, 498906, 20671542 ... etc.
In this article, we will see how to check if a number is a Tcefrep number by using Java programming language.
To show you some instances
Instance-1
Input number is 498906.
Let’s check it by using the logic of Tcefrep number.
Reverse of the given number= 609894.
Proper divisors of given number 498906 are= 1, 2, 3, 6, 9, 18, 27, 54, 9239, 18478, 27717, 55434, 83151, 166302, 249453.
Sum of above numbers = 609894
As we notice here both the calculated sum value and reverse of the original number are same.
Hence, 498906 is a Tcefrep number.
Instance-2
Input number is 20671542.
Let’s check it by using the logic of Tcefrep number.
Reverse of the given number= 24517602.
Proper divisors of given number 498906 are= 1 ,2 ,3 ,6 ,9 ,18 ,113 ,226 ,339 ,678 ,1017 ,2034 ,10163 ,20326 ,30489 ,60978 ,91467 ,182934 ,1148419 ,2296838 ,3445257 ,6890514 ,10335771.
Sum of above numbers = 24517602
As we notice here both the calculated sum value and reverse of the original number are same.
Hence, 20671542 is a Tcefrep number.
Instance-3
Input number is 12343233.
Let’s check it by using the logic of Tcefrep number.
Reverse of the given number= 33234321.
Proper divisors of given number 498906 are= 1 ,3 ,7 ,21 ,587773 ,1763319 ,4114411.
Sum of above numbers = 18808768
As we notice here both the calculated sum value and reverse of the original number are not same.
Hence, 12343233 is not a Tcefrep number.
Algorithm
Step-1 - Get the input number either by static input or by user input.
Step-2 - Find the reverse of the original number.
Step-3 - Now find all the possible proper divisors of the original number and calculate the sum of it.
Step-4 - Then compare both the reverse value with the calculated sum value.
Step-5 - If both the values are same, then the input number is called as Tcefrep number else not.
语法
To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.
Following is the syntax to get square root of any number by using the method -
double squareRoot = Math.sqrt(input_vale)
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Input Value with User Defined Method
By Using User Input Value with User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input Value with User Defined Method
In this approach, we declare a variable and initialize it with a positive number and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can check whether the number is a Tcefrep number or not.
示例
public class Main {
//main 方法
public static void main(String[] args) {
//声明一个变量并通过静态输入方法存储一个值
int inputNumber = 20671542;
//调用用户定义方法来检查 Tcefrep 数字
if (checkTcefrep(inputNumber))
System.out.print(inputNumber+" 是一个 Tcefrep 数字。");
else
System.out.print(inputNumber+" 不是一个 Tcefrep 数字。");
}
//用户定义方法来查找输入数字的反向
static int reverseNumber(int n) {
//声明一个变量来存储反向数字
int rev = 0;
//继续循环直到 n 变为零
while(n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
//返回反转的数字
return rev;
}
// 用户定义方法来找到所有适当的除数并返回和值
static int properDivisorsSum(int n) {
// 声明一个变量来存储总和值
int sum = 0;
// 循环查找除数
for (int i = 2; i<= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i))
sum += i;
else
sum += (i + n / i);
}
}
// 我们找到除 1 之外的除数,因此我们将最终总和值加 1
return (sum + 1);
}
//用户定义方法来检查 Tcefrep 数字
static boolean checkTcefrep(int n) {
//如果条件满足则返回 true
//这里我们调用用户定义的方法 propDivisorsSum()
//计算所有适当除数的总和并调用 reverseNumber() 方法
//获取原始数字的反向值
//如果两种方法的结果相同则返回 true,否则返回 false
return propDivisorsSum(n) == reverseNumber(n);
}
}
输出
20671542 是一个 Tcefrep 数字。
方法 2:通过使用用户输入值和用户定义方法
在这种方法中,我们声明一个变量并将一个正数作为用户输入,并将该数字作为参数传递给用户定义的方法,然后在方法内部使用算法我们可以检查该数字是否是 Tcefrep 数字。
示例
import java.util.*;
public class Main {
//main 方法
public static void main(String[] args) {
//创建 Scanner 类的对象
Scanner sc = new Scanner(System.in);
//要求用户输入一个数字
System.out.print("输入一个数字:");
//声明一个变量并根据用户输入获取值
int inputNumber = sc.nextInt();
//调用用户定义的方法来检查 Tcefrep 数字
if (checkTcefrep(inputNumber))
System.out.print(inputNumber+" 是一个 Tcefrep 数字。");
else
System.out.print(inputNumber+" 不是一个 Tcefrep 数字。");
}
//用户定义方法查找输入数字的逆序
static int reverseNumber(int n) {
//声明一个变量来存储逆序数字
int rev = 0;
//继续循环直到 n 变为零
while(n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
//返回逆序数字
return rev;
}
// 用户定义方法,用于查找所有适当除数并返回总和值
static int propagateDivisorsSum(int n) {
// 声明一个变量来存储总和值
int sum = 0;
// 循环查找除数
for (int i = 2; i<= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i))
sum += i;
else
sum += (i + n / i);
}
}
// 我们找到除 1 以外的除数,因此我们将最终总和值加 1
return (sum + 1);
}
//用户定义方法检查 Tcefrep 数字
static boolean checkTcefrep(int n) {
//如果条件满足则返回 true
//这里我们调用用户定义方法 propDivisorsSum()
//计算所有适当除数的总和并调用 reverseNumber() 方法
//获取原始数字的反向值
//如果两种方法的结果相同则返回 true,否则返回 false
return propDivisorsSum(n) == reverseNumber(n);
}
}
输出
输入一个数字:6 6 是一个 Tcefrep 数字。
在本文中,我们探讨了如何使用不同的方法在 Java 中检查一个数字是否是 Tcefrep 数字
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

