如何在 Java 中檢查一個數是否為 Insolite 數?
如果一個數能被其每位數字平方和以及所有數字乘積的平方整除,則稱該數為 Insolite 數。
為了更清楚地說明,我們需要找到每位數字的平方和。然後我們需要將給定數字的每位數字相乘,之後計算其平方值。如果給定數字能被這兩個結果值整除,那麼我們可以說給定數字是 Insolite 數。
在這篇文章中,我們將學習如何使用 Java 程式語言來檢查一個數是否為 Insolite 數。
舉幾個例子
例項 1
輸入數字為 1122112。
讓我們使用 Insolite 數的邏輯來檢查它。
1122112 每位數字平方和 =
= (1^2) + (1^2) ++ (2^2) + (2^2) + (1^2) + (1^2) + (2^2) = 1 + 1 + 4 + 4 + 1 + 1 + 4 = 16
1122112 所有數字乘積的平方 =
=(1 * 1 * 2 * 2 * 1 * 1 * 2 ) ^ 2 = 64
1122112 可以被 16 和 64 整除。
因此,1122112 是一個 Insolite 數。
例項 2
輸入數字為 123123。
讓我們使用 Insolite 數的邏輯來檢查它。
123123 每位數字平方和 =
=(1^2) + (2^2) ++ (3^2) + (1^2) + (2^2) + (3^2) = 1 + 4 + 9 + 1 + 4 + 9 = 28
123123 所有數字乘積的平方 =
=(1 * 2 * 3 * 1 * 2 * 3) ^ 2 = 1296
123123 不能同時被 28 和 1296 整除。
因此,123123 不是一個 Insolite 數。
例項 3
輸入數字為 121212。
讓我們使用 Insolite 數的邏輯來檢查它。
121212 每位數字平方和 =
= (1^2) + (2^2) ++ (1^2) + (2^2) + (1^2) + (2^2) = 1 + 4 + 1 + 4 + 1 + 4 = 15
121212 所有數字乘積的平方 =
= (1 * 2 * 1 * 2 * 1 * 2) ^ 2 = 64
121212 不能同時被 15 和 64 整除。
因此,123123 不是一個 Insolite 數。
語法
在 Java 中,我們可以使用內建的 java.lang.Math.pow() 方法來獲取任何數的冪。
以下是使用該方法獲取 2 的冪的語法
double power = Math.pow(inputValue,2)
演算法
步驟 1 - 獲取一個整數,可以透過初始化或使用者輸入獲得。
步驟 2 - 然後迴圈,直到二進位制數等於零。
步驟 3 - 計算每位數字平方的和與積。
步驟 4 - 最後,檢查結果值是否可以被輸入數字整除。
步驟 5 - 如果可以整除,則得出結論,該數字是 Insolite 數,否則該數字不是 Insolite 數。
多種方法
我們提供了多種解決方法。
使用靜態輸入值
使用使用者定義的方法
讓我們逐一檢視程式及其輸出。
方法 1:使用靜態輸入值
在這種方法中,程式中將初始化一個整數值,然後使用演算法檢查該數是否為 Insolite 數。
示例
import java.util.*; import java.lang.Math; public class Main{ //main method public static void main (String[] args){ //declare a variable with a static input value int inputNumber = 1122112 ; //store the input number into an another variable int temp = inputNumber; // for storing the addition value declare a variable int sum = 0; // for storing the multiplication value declare a variable int prod = 1; //loop to calculate both sum and product value while (temp != 0){ // take digits from unit place int i = temp % 10; //calculate the sum value sum = sum + (int)Math.pow(i, 2); //calculate the multiplication value prod = prod * (int)Math.pow(i, 2); //removing that unit place value temp = temp / 10; } //check the condition //whether both resultant values are divisible by inputNumberor not if((inputNumber % sum == 0) && (inputNumber % prod == 0)) System.out.print(inputNumber + " is an Insolite Number."); else System.out.print(inputNumber + " is not an Insolite Number."); } }
輸出
1122112 is an Insolite Number.
方法 2:使用使用者定義的方法
在這種方法中,將提示使用者輸入一個整數值,然後我們將透過將此輸入數字作為引數傳遞給使用者定義的方法。
在方法內部,我們將使用演算法檢查該數是否為 Insolite 數。
示例
public class Main{ //main method public static void main (String[] args){ //declare an int variable and initialize a number int inputNumber =126317; System.out.println("Given number: "+inputNumber); //call the user defined method inside conditional statement if (checkInsolite(inputNumber)){ //if its true System.out.println(inputNumber + " is an Insolite Number."); } else { //if its true System.out.println(inputNumber + " is not an Insolite Number."); } } // define the user defined method // check for Insolitenumber static boolean checkInsolite(int inputNumber){ //store the input number into an another variable int temp = inputNumber; // for storing the addition value declare a variable int sum = 0; // for storing the multiplication value declare a variable int prod = 1; //loop to calculate both sum and product value while (temp != 0){ // take digits from unit place int i = temp % 10; //calculate the sum value sum = sum + i * i; //calculate the multiplication value prod = prod * i * i; //removing that unit place value temp = temp / 10; } //check the condition //whether both resultant values are divisible by inputNumberor not if((inputNumber % sum == 0) && (inputNumber % prod == 0)) return true; else return false; } }
輸出
Given number: 126317 126317 is not an Insolite Number.
在這篇文章中,我們探討了如何在 Java 中使用不同的方法來檢查一個數是否為 Insolite 數。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP