讲师中心 微信公众号
AI工具推荐 视频效率加速

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

风敏大大_9717

风敏大大_9717

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

|

1539人浏览过

|

来源于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.

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

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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

PixTV
PixTV Hot

PixTV是一款面向AIGC内容创作的AI视频生成工具。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

Laper
Laper Hot

Laper是专为编剧、导演和制片人推出的 AI 原生剧本创作工具。

咔片AIPPT

一款在线AI演示文稿制作工具,可根据主题和内容需求辅助生成PPT结构与页面,提高演示材料制作效率。

SkildArt
SkildArt Hot

SkildArt是一款AI文本写作工具,一站式 AI 视觉创作平台。

AionClaw
AionClaw Hot

AionClaw是一款面向办公、创作和编程任务的AI桌面智能体。

相关专题

更多
typedef和define区别
typedef和define区别

typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

313

2023.09.26

define的用法
define的用法

define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

619

2023.10.11

function是什么
function是什么

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果。本专题为大家提供function是什么的相关的文章、下载、课程内容,供大家免费下载体验。

3020

2023.08.04

js函数function用法
js函数function用法

js函数function用法有:1、声明函数;2、调用函数;3、函数参数;4、函数返回值;5、匿名函数;6、函数作为参数;7、函数作用域;8、递归函数。本专题提供js函数function用法的相关文章内容,大家可以免费阅读。

514

2023.10.07

点击input框没有光标怎么办
点击input框没有光标怎么办

点击input框没有光标的解决办法:1、确认输入框焦点;2、清除浏览器缓存;3、更新浏览器;4、使用JavaScript;5、检查硬件设备;6、检查输入框属性;7、调试JavaScript代码;8、检查页面其他元素;9、考虑浏览器兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

794

2023.11.24

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

5716

2023.08.14

Linux 新手先学哪些命令
Linux 新手先学哪些命令

Linux新手命令专题汇集终端基础、文件目录操作、文本查看、软件安装和权限管理等入门文章,帮助按实际任务练习常用命令并建立命令行操作习惯。

0

2026.10.15

MySQL 忘记密码怎么办
MySQL 忘记密码怎么办

MySQL忘记密码专题汇集不同版本与系统下的root密码重置方法,覆盖MySQL 5.7、8.0、Mac、Linux及本地集成环境,并提示恢复权限与重新登录的检查步骤。

0

2026.10.15

Vue.js 中 ref 和 reactive 的区别
Vue.js 中 ref 和 reactive 的区别

Vue.js中ref和reactive的区别专题汇集两种响应式API的定义、访问方式、对象解构、侦听与性能取舍等文章,帮助结合实际状态类型选择合适写法。

0

2026.10.15

热门下载

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

精品课程

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

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn