如何在 Java 中檢查一個數是否為醜數?
如果一個數字的質因數只包含 2、3 和 5,則稱該數字為醜數。
也許有些數字的質因數只有一個或兩個因數,但這些因數必須是 2、3 和 5 中的一個。
在這篇文章中,我們將瞭解如何使用 Java 程式語言來檢查一個數字是否為醜數。
舉幾個例子
示例 1
輸入數字為 20。
讓我們用醜數的邏輯來檢查它。
Prime factors of 20 = 2, 2, 5
因此,正如您在此處注意到的,所有質因數都只包含 2、3 和 5 中的一個。
因此,20 是一個醜數。
示例 2
輸入數字為 30。
讓我們用醜數的邏輯來檢查它。
Prime factors of 30 = 2, 3, 5
因此,正如您在此處注意到的,所有質因數都只包含 2、3 和 5 中的一個。
因此,30 是一個醜數。
示例 3
輸入數字為 14。
讓我們用醜數的邏輯來檢查它。
Prime factors of 14 = 2, 7
因此,正如您在此處注意到的,7 出現在上述質因數之一中。
因此,14 不是一個醜數。
演算法
步驟 1 - 首先,我們定義一個函式來檢查輸入數字是否為質數。
步驟 2 - 從使用者那裡收集輸入,可以使用靜態方法或使用者定義的方法。
步驟 3 - 初始化迴圈以查詢輸入數字的所有質因數。
步驟 4 - 找到質因數後,我們執行一個條件來檢查這些因數是否僅包含 2、3 和 5。
步驟 5 - 如果條件為真,則列印輸入數字是一個醜數,否則輸入數字不是一個醜數。
多種方法
我們提供了不同方法的解決方案。
使用靜態輸入值
使用使用者定義的方法
讓我們逐一檢視 Java 程式及其輸出。
方法 1:使用靜態輸入值
在這種方法中,程式將初始化一個非零正整數,然後使用演算法檢查該數字是否為醜數。
示例
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
方法 2:使用使用者定義的方法
在這種方法中,程式將初始化一個非零正整數,然後透過將該輸入數字作為引數傳遞來呼叫使用者定義的方法。
在方法內部,我們將使用演算法檢查該數字是否為醜數。
示例
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 number
在本文中,我們探討了如何使用不同的方法在 Java 中檢查一個數字是否為醜數。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP