如果一個數的質因數只包括2、3和5,那麼它被稱為醜數。
也許某些數字具有僅有一個或兩個因子的質因數,但這些因子必須是2、3和5之一。
In this article we will see how to check if a number is an Ugly number or not by using Java programming language.
Input number is 20.
讓我們使用醜數的邏輯來檢查一下。
Prime factors of 20 = 2, 2, 5
所以,正如你在這裡注意到的,所有的質因數只包括2、3和5中的任意一個。
Hence, 20 is an ugly number.
Input number is 30.
讓我們使用醜數的邏輯來檢查一下。
Prime factors of 30 = 2, 3, 5
所以,正如你在這裡注意到的,所有的質因數只包括2、3和5中的任意一個。
Hence, 30 is an ugly number.
Input number is 14.
讓我們使用醜數的邏輯來檢查一下。
Prime factors of 14 = 2, 7
So, as you notice here 7 is present at one of the prime factors above.
Hence, 14 is not an ugly number.
Step 1 − First we define a function for checking if the input number is a prime number or not.
#步驟2 - 透過靜態方法或使用者定義的方法收集使用者的輸入。
步驟 3 − 初始化循環以找到輸入數字的所有質因數。
第4步 - 找到質因數後,我們執行一個條件來檢查因子是否只包含2、3和5。
Step 5 − If the condition is true then we are printing the input number is an ugly number otherwise the input number is not an ugly number.
我們以不同的方法提供了解決方案。
透過使用靜態輸入值
#By Using User Defined Method
#讓我們逐一查看Java程式及其輸出。
In this approach one non−zero, positive integer value will be initialized in the program and then by using the algorithm we can check whether a number is an ugly number or not
.import java.util.Scanner; public class Main { public static void main(String args[]) { //declare a variable with a static value int inputNumber = 25; //declare a variable with boolean value boolean check = true; //initialise the loop for the checking of ugly number for(int i = 2; i<=inputNumber; i++) { // checks whether numbers only include 2,3 and 5 if(i!=2&&i!=3&&i!=5) { // Checks if there are some other prime factors if(inputNumber%i==0&&checkPrime(i)) { // Sets the flag to false if there are some other prime factors check = false; break; } } } if(check) { System.out.println(inputNumber+" is an ugly number"); } else { System.out.println(inputNumber+" is Not an ugly number"); } } // Function that checks for prime static boolean checkPrime(int number) { boolean flag = true; for(int i = 2; i<=number/2; i++) { if(number%i==0) { flag = false; break; } } return flag; } }
25 is an ugly number
In this approach one non−
zero, positive integer value will be initialized in the program and then we will call a user defined method by passing this input number as parameter.在這個方法中,我們將使用演算法來檢查一個數字是否是一個醜數
。import java.util.Scanner; public class Main { //initialise main method public static void main(String[] args) { //declare a variable with a static value int inp = 56; //check whether our condition is true or not in user defined method //if true number is ugly otherwise not if(checkUgly(inp)) { System.out.println(inp+" is an ugly number"); } else { System.out.println(inp+" is Not an ugly number"); } } // Function that checks for prime static boolean checkPrime(int number) { boolean flag = true; for(int i = 2; i<=number/2; i++) { if(number%i==0) { flag = false; break; } } return flag; } //define the user defined method //checking for ugly number public static boolean checkUgly(int inputNumber) { //declare a variable with boolean value boolean check = true; //initialise the loop for the checking of Ugly number for(int i = 2; i<=inputNumber; i++) { // checks whether numbers only include 2,3 and 5 if(i!=2&&i!=3&&i!=5) { // Checks if there are some other prime factors if(inputNumber%i==0&&checkPrime(i)) { // Sets the flag to false if there are some other prime factors return false; } } } return true; } }
56 is Not an ugly numberIn this article, we explored how to check a number whether it is an ugly number or not in Java by using different approaches.###
以上是如何在Java中檢查一個數字是否為醜數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!