Tcefrep Numbers in Java
If the reverse of a number equals the sum of the accurate divisors of the original number, the number is said to be a Tcefrep number.
To further explain, we calculate the original number's reverse and the sum of its divisors. The given number is a TCEFREP number if both the sum value and the reverse of the number provided are the same.
Example 1:
Input:
int num = 6
Output:
Yes, the given number is the Tcefrep Number.
Explanation:
The given number is 6, and the reverse of it is given by 6. The proper divisors of 498906 are 1, 2, and 3. Now, the sum of the proper divisors is equal to 6. Hence, the obtained sum equals the given number's reverse. Therefore, the given number is the Tcefrep Number.
Example 2:
Input:
int num = 498906
Output:
Yes, the given number is the Tcefrep Number.
Explanation:
The given number is 498906, and the reverse of it is given by 609894. The proper divisors of the given number 498906 are 1, 2, 3, 6, 9, 18, 27, 54, 9239, 18478, 27717, 55434, 83151, 166302, 249453. Now, the sum of the proper divisors is equal to 609894. Hence, the obtained sum equals the given number's reverse. Therefore, the given number is the Tcefrep Number.
Example 3:
Input:
int num = 120
Output:
No, the given number is the Tcefrep Number.
Explanation:
The given number is 120, and the reverse of it is given by 021. The proper divisors of the given number 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120. Now, the sum of the proper divisors is equal to 360. Hence, the obtained sum is not equal to the reverse of the given number. Therefore, the given number is not a Tcefrep Number.
Approach: Using Naïve Method
Algorithm:
Step -1: First, determine the sum of proper divisors of the given number.
Step 2: We shall find the reverse of the given number.
Step 3: Next, we will determine whether the sum of the given number's proper divisors equals the given number's reverse.
Step -4: If yes, then it is a Tcefrep number. Else, it is not a Tcefrep number.
Implementation:
FileName: Tcefrep.java
import java.io.*; import java.util.*; public class Tcefrep { // Reversing the digits of a number // using an iterative function static int ReverseNumber(int num) { int rev = 0; int rem; while(num > 0) { rem = num % 10; rev = rev * 10 + rem; num = num / 10; } return rev; } // Calculates the sum of all suitable divisors // using the given natural number and the function num. static int properDivSum(int num) { // Final result of adding the divisors int res = 0; // Determine all divisors that divide the given number for (int i = 2; i<= Math.sqrt(num); i++) { // if 'i' is the divisor of 'num' if (num % i == 0) { //Add if both divisors are the same // only once, else, add both if (i == (num / i)) res += i; else res += (i + num / i); } } // Since 1 is also a divisor, add 1 to the res. return (res + 1); } static boolean TcefrepNum(int num) { return properDivSum(num) == ReverseNumber(num); } public static void main(String[] args) { int num = 6; if (TcefrepNum(num)) System.out.print("Yes, the given number is Tcefrep Number."); else System.out.print("No, the given number is Tcefrep Number."); } }
Output:
Yes, the given number is the Tcefrep Number.