首頁 > Java > java教程 > 主體

如何在Java中檢查一個數字是否為醜數?

WBOY
發布: 2023-08-20 13:25:07
轉載
1327 人瀏覽過

如何在Java中檢查一個數字是否為醜數?

如果一個數的質因數只包括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.

To show you some instances

Instance-1

Input number is 20.

讓我們使用醜數的邏輯來檢查一下。

Prime factors of 20 = 2, 2, 5
登入後複製

所以,正如你在這裡注意到的,所有的質因數只包括2、3和5中的任意一個。

Hence, 20 is an ugly number.

Instance-2

Input number is 30.

讓我們使用醜數的邏輯來檢查一下。

Prime factors of 30 = 2, 3, 5
登入後複製

所以,正如你在這裡注意到的,所有的質因數只包括2、3和5中的任意一個。

Hence, 30 is an ugly number.

Instance-3

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.

Algorithm

  • 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.

#Multiple Approaches

我們以不同的方法提供了解決方案。

  • 透過使用靜態輸入值

  • #By Using User Defined Method

#讓我們逐一查看Java程式及其輸出。

Approach-1: By Using Static Input Value

In this approach one nonzero, 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

.

Example

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
登入後複製

Approach-2: By Using User Defined Method

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.

在這個方法中,我們將使用演算法來檢查一個數字是否是一個醜數

Example

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
登入後複製
In 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中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板