首页  >  文章  >  Java  >  如何在Java中检查一个数字是否是丑数?

如何在Java中检查一个数字是否是丑数?

WBOY
WBOY转载
2023-08-20 13:25:071320浏览

如何在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 nonzero, 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删除