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.

PathFinder
PathFinder

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

下载

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

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

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

119

2023.09.26

define的用法
define的用法

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

389

2023.10.11

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

847

2023.08.22

function是什么
function是什么

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

499

2023.08.04

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

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

166

2023.10.07

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

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

197

2023.11.24

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

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

497

2023.08.14

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

37

2026.03.12

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

136

2026.03.11

热门下载

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

精品课程

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

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