如何在Java中檢查一個數是否為Tcefrep數?
如果一個數的反轉等於其真因數之和,則稱該數為Tcefrep數。
為了更清楚地說明,我們找到一個數的因數之和,並找到原始數的反轉。如果和值和給定數的反轉相同,那麼我們可以說給定數是一個Tcefrep數。
一些Tcefrep數的例子是:6、498906、20671542……等等。
在本文中,我們將瞭解如何使用Java程式語言來檢查一個數是否為Tcefrep數。
為您展示一些例項
例項1
輸入數字為498906。
讓我們使用Tcefrep數的邏輯來檢查它。
給定數字的反轉= 609894。
給定數字498906的真因數為= 1、2、3、6、9、18、27、54、9239、18478、27717、55434、83151、166302、249453。
以上數字之和 = 609894
正如我們在這裡看到的,計算出的和值和原始數字的反轉相同。
因此,498906是一個Tcefrep數。
例項2
輸入數字為20671542。
讓我們使用Tcefrep數的邏輯來檢查它。
給定數字的反轉= 24517602。
給定數字498906的真因數為= 1、2、3、6、9、18、113、226、339、678、1017、2034、10163、20326、30489、60978、91467、182934、1148419、2296838、3445257、6890514、10335771。
以上數字之和 = 24517602
正如我們在這裡看到的,計算出的和值和原始數字的反轉相同。
因此,20671542是一個Tcefrep數。
例項3
輸入數字為12343233。
讓我們使用Tcefrep數的邏輯來檢查它。
給定數字的反轉= 33234321。
給定數字498906的真因數為= 1、3、7、21、587773、1763319、4114411。
以上數字之和 = 18808768
正如我們在這裡看到的,計算出的和值和原始數字的反轉不相同。
因此,12343233不是一個Tcefrep數。
演算法
步驟1 - 透過靜態輸入或使用者輸入獲取輸入數字。
步驟2 - 找到原始數字的反轉。
步驟3 - 現在找到原始數字的所有可能的真因數並計算其和。
步驟4 - 然後將反轉值與計算出的和值進行比較。
步驟5 - 如果兩個值相同,則輸入數字稱為Tcefrep數,否則不是。
語法
要獲取一個數的平方根,我們在java.lang包的Math類中有一個內建的sqrt()方法。
以下是使用該方法獲取任何數的平方根的語法 -
double squareRoot = Math.sqrt(input_vale)
多種方法
我們提供了不同方法的解決方案。
使用靜態輸入值和使用者定義方法
使用使用者輸入值和使用者定義方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值和使用者定義方法
在這種方法中,我們宣告一個變數並用一個正數初始化它,並將此數字作為引數傳遞給使用者定義的方法,然後在方法內部使用演算法,我們可以檢查該數字是否為Tcefrep數。
示例
public class Main {
//main method
public static void main(String[] args) {
//declare a variable and store a value by static input method
int inputNumber = 20671542;
//call the user-defined method to check for Tcefrep number
if (checkTcefrep(inputNumber))
System.out.print(inputNumber+" is a Tcefrep number.");
else
System.out.print(inputNumber+" is not a Tcefrep number.");
}
//user-defined method to find the reverse of input number
static int reverseNumber(int n) {
//declare a variable to store the reverse number
int rev = 0;
//continue the loop till n becomes zero
while(n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
//return the reverse number
return rev;
}
// user-defined method to find all the proper divisors and return the sum value
static int properDivisorsSum(int n) {
// Declare a variable to store the sum value
int sum = 0;
// Loop to find the divisors
for (int i = 2; i<= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i))
sum += i;
else
sum += (i + n / i);
}
}
// We find divisors excluding 1 so we add one with final sum value
return (sum + 1);
}
//user-defined method to check the Tcefrep number
static boolean checkTcefrep(int n) {
//return true if condition satisfied
//here we are calling user defined method properDivisorsSum()
//for sum of all proper divisors and calling reverseNumber() method
//to get the reverse value of original number
//and returning true if the result of both methods are same else return false
return properDivisorsSum(n) == reverseNumber(n);
}
}
輸出
20671542 is a Tcefrep number.
方法2:使用使用者輸入值和使用者定義方法
在這種方法中,我們宣告一個變數並獲取使用者輸入的正數,並將此數字作為引數傳遞給使用者定義的方法,然後在方法內部使用演算法,我們可以檢查該數字是否為Tcefrep數。
示例
import java.util.*;
public class Main {
//main method
public static void main(String[] args) {
//create object of Scanner class
Scanner sc = new Scanner(System.in);
//Ask user to enter a number
System.out.print("Enter a number: ");
//declare a variable and take the value by user input
int inputNumber = sc.nextInt();
//call the user-defined method to check for Tcefrep number
if (checkTcefrep(inputNumber))
System.out.print(inputNumber+" is a Tcefrep number.");
else
System.out.print(inputNumber+" is not a Tcefrep number.");
}
//user-defined method to find the reverse of input number
static int reverseNumber(int n) {
//declare a variable to store the reverse number
int rev = 0;
//continue the loop till n becomes zero
while(n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
//return the reverse number
return rev;
}
// user-defined method to find all the proper divisors and return the sum value
static int properDivisorsSum(int n) {
// Declare a variable to store the sum value
int sum = 0;
// Loop to find the divisors
for (int i = 2; i<= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == (n / i))
sum += i;
else
sum += (i + n / i);
}
}
// We find divisors excluding 1 so we add one with final sum value
return (sum + 1);
}
//user-defined method to check the Tcefrep number
static boolean checkTcefrep(int n) {
//return true if condition satisfied
//here we are calling user defined method properDivisorsSum()
//for sum of all proper divisors and calling reverseNumber() method
//to get the reverse value of original number
//and returning true if the result of both methods are same else return false
return properDivisorsSum(n) == reverseNumber(n);
}
}
輸出
Enter a number: 6 6 is a Tcefrep number.
在本文中,我們探討了如何在Java中使用不同的方法來檢查一個數字是否為Tcefrep數。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP