0

0

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

WBOY

WBOY

发布时间:2023-08-20 13:25:07

|

1519人浏览过

|

来源于tutorialspoint

转载

如何在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.

立即学习Java免费学习笔记(深入)”;

让我们使用丑数的逻辑来检查一下。

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.

Musico
Musico

Musico 是一个AI驱动的软件引擎,可以生成音乐。 它可以对手势、动作、代码或其他声音做出反应。

下载

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
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

805

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

724

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

727

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

395

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

445

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

428

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16861

2023.08.03

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.4万人学习

中谷教育Python视频教程
中谷教育Python视频教程

共38课时 | 8.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号