C#,阿格里数(Ugly Number)的多种算法与源代码

news2024/10/6 4:04:38

1 丑数,阿格里数

阿格里数,即丑数(Ugly Number)、逊数(Humble Number)。

一般而言:把只包含质因子2,3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但7、14不是,因为它们包含质因子7。 习惯上我们把1当做是第一个丑数。
拓展一下:对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S。这个正整数集合包括,p1、p1*p2、p1*p1、p1*p2*p3...(还有其它)。该集合被称为S集合的“丑数集合”。

计算结果:

2 源程序,文本格式

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    public static partial class Number_Sequence
    {
        private static int UglyNumber_MaxDivide(int a, int b)
        {
            while (a % b == 0)
            {
                a = a / b;
            }
            return a;
        }


        public static int IsUglyNumber_Original(int no)
        {
            no = UglyNumber_MaxDivide(no, 2);
            no = UglyNumber_MaxDivide(no, 3);
            no = UglyNumber_MaxDivide(no, 5);

            return (no == 1) ? 1 : 0;
        }

        public static int Ugly_Number(int index)
        {
            int i = 1;
            int count = 1;
            while (index > count)
            {
                i++;
                if (IsUglyNumber_Original(i) == 1)
                {
                    count++;
                }
            }
            return i;
        }


        public static int Ugly_Number_Second(int n)
        {
            int[] ugly = new int[n];
            int i2 = 0, i3 = 0, i5 = 0;
            int next_multiple_of_2 = 2;
            int next_multiple_of_3 = 3;
            int next_multiple_of_5 = 5;
            int next_ugly_no = 1;
            ugly[0] = 1;

            for (int i = 1; i < n; i++)
            {
                next_ugly_no = Math.Min(next_multiple_of_2, Math.Min(next_multiple_of_3, next_multiple_of_5));

                ugly[i] = next_ugly_no;

                if (next_ugly_no == next_multiple_of_2)
                {
                    i2 = i2 + 1;
                    next_multiple_of_2 = ugly[i2] * 2;
                }

                if (next_ugly_no == next_multiple_of_3)
                {
                    i3 = i3 + 1;
                    next_multiple_of_3 = ugly[i3] * 3;
                }
                if (next_ugly_no == next_multiple_of_5)
                {
                    i5 = i5 + 1;
                    next_multiple_of_5 = ugly[i5] * 5;
                }
            }
            return next_ugly_no;
        }

        public static int Ugly_Number_Third(int n)
        {
            SortedSet<int> t = new SortedSet<int>();
            t.Add(1);
            int i = 1;
            while (i < n && t.Count > 0)
            {
                int temp = t.Min;
                t.Remove(temp);
                t.Add(temp * 2);
                t.Add(temp * 3);
                t.Add(temp * 5);
                i++;
            }

            return t.Min;
        }

        private static int Ugly_Number_UpperBound(int[] array, int left, int right, int element)
        {
            while (left < right)
            {
                int middle = left + (right - left) / 2;
                if (array[middle] > element)
                {
                    right = middle;
                }
                else
                {
                    left = middle + 1;
                }
            }
            return left;
        }

        public static int Ugly_Number_Fourth(int n)
        {
            int[] pow = new int[40];
            for (int i = 0; i < 40; ++i)
            {
                pow[i] = 1;
            }
            for (int i = 1; i <= 30; ++i)
            {
                pow[i] = pow[i - 1] * 2;
            }
            int left = 1;
            int right = 2147483647;
            int ans = -1;
            while (left <= right)
            {
                int mid = left + ((right - left) / 2);
                int cnt = 0;
                for (long i = 1; i <= mid; i *= 5)
                {
                    for (long j = 1; j * i <= mid; j *= 3)
                    {
                        cnt += Ugly_Number_UpperBound(pow, 0, 31, (int)(mid / (i * j)));
                    }
                }
                if (cnt < n)
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                    ans = mid;
                }
            }

            return ans;
        }
    }
}
 

————————————————————

POWER BY TRUFFER.CN

3 代码格式

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    public static partial class Number_Sequence
    {
        private static int UglyNumber_MaxDivide(int a, int b)
        {
            while (a % b == 0)
            {
                a = a / b;
            }
            return a;
        }


        public static int IsUglyNumber_Original(int no)
        {
            no = UglyNumber_MaxDivide(no, 2);
            no = UglyNumber_MaxDivide(no, 3);
            no = UglyNumber_MaxDivide(no, 5);

            return (no == 1) ? 1 : 0;
        }

        public static int Ugly_Number(int index)
        {
            int i = 1;
            int count = 1;
            while (index > count)
            {
                i++;
                if (IsUglyNumber_Original(i) == 1)
                {
                    count++;
                }
            }
            return i;
        }


        public static int Ugly_Number_Second(int n)
        {
            int[] ugly = new int[n];
            int i2 = 0, i3 = 0, i5 = 0;
            int next_multiple_of_2 = 2;
            int next_multiple_of_3 = 3;
            int next_multiple_of_5 = 5;
            int next_ugly_no = 1;
            ugly[0] = 1;

            for (int i = 1; i < n; i++)
            {
                next_ugly_no = Math.Min(next_multiple_of_2, Math.Min(next_multiple_of_3, next_multiple_of_5));

                ugly[i] = next_ugly_no;

                if (next_ugly_no == next_multiple_of_2)
                {
                    i2 = i2 + 1;
                    next_multiple_of_2 = ugly[i2] * 2;
                }

                if (next_ugly_no == next_multiple_of_3)
                {
                    i3 = i3 + 1;
                    next_multiple_of_3 = ugly[i3] * 3;
                }
                if (next_ugly_no == next_multiple_of_5)
                {
                    i5 = i5 + 1;
                    next_multiple_of_5 = ugly[i5] * 5;
                }
            }
            return next_ugly_no;
        }

        public static int Ugly_Number_Third(int n)
        {
            SortedSet<int> t = new SortedSet<int>();
            t.Add(1);
            int i = 1;
            while (i < n && t.Count > 0)
            {
                int temp = t.Min;
                t.Remove(temp);
                t.Add(temp * 2);
                t.Add(temp * 3);
                t.Add(temp * 5);
                i++;
            }

            return t.Min;
        }

        private static int Ugly_Number_UpperBound(int[] array, int left, int right, int element)
        {
            while (left < right)
            {
                int middle = left + (right - left) / 2;
                if (array[middle] > element)
                {
                    right = middle;
                }
                else
                {
                    left = middle + 1;
                }
            }
            return left;
        }

        public static int Ugly_Number_Fourth(int n)
        {
            int[] pow = new int[40];
            for (int i = 0; i < 40; ++i)
            {
                pow[i] = 1;
            }
            for (int i = 1; i <= 30; ++i)
            {
                pow[i] = pow[i - 1] * 2;
            }
            int left = 1;
            int right = 2147483647;
            int ans = -1;
            while (left <= right)
            {
                int mid = left + ((right - left) / 2);
                int cnt = 0;
                for (long i = 1; i <= mid; i *= 5)
                {
                    for (long j = 1; j * i <= mid; j *= 3)
                    {
                        cnt += Ugly_Number_UpperBound(pow, 0, 31, (int)(mid / (i * j)));
                    }
                }
                if (cnt < n)
                {
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                    ans = mid;
                }
            }

            return ans;
        }
    }
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1426729.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于PSO-BP神经网络的风电功率MATLAB预测程序

微❤关注“电气仔推送”获得资料&#xff08;专享优惠&#xff09; 参考文献 基于风电场运行特性的风电功率预测及应用分析——倪巡天 资源简介 由于自然风具有一定的随机性、不确定性与波动性&#xff0c;这将会使风电场的功率预测受到一定程度的影响&#xff0c;它们之间…

FPGA高端项目:Xilinx Zynq7020系列FPGA 多路视频缩放拼接 工程解决方案 提供4套工程源码+技术支持

目录 1、前言版本更新说明给读者的一封信FPGA就业高端项目培训计划免责声明 2、相关方案推荐我这里已有的FPGA图像缩放方案我已有的FPGA视频拼接叠加融合方案本方案的Xilinx Kintex7系列FPGA上的ov5640版本本方案的Xilinx Kintex7系列FPGA上的HDMI版本本方案的Xilinx Artix7系列…

探索设计模式的魅力:从单一继承到组合模式-软件设计的演变与未来

设计模式专栏&#xff1a;http://t.csdnimg.cn/nolNS 在面对层次结构和树状数据结构的软件设计任务时&#xff0c;我们如何优雅地处理单个对象与组合对象的一致性问题&#xff1f;组合模式&#xff08;Composite Pattern&#xff09;为此提供了一种简洁高效的解决方案。通过本…

C++类与对象:默认成员函数

文章目录 1.类的6个默认成员函数2.构造函数3.析构函数4. 拷贝构造函数5.赋值运算符和运算符重载6.日期类实现7.const成员8.重载流插入<< &#xff0c;流提取>>1.流插入2.流提取 9.取地址及const取地址操作符重载 1.类的6个默认成员函数 空类:也就是什么成员都没有的…

Spring Boot集成Redisson详细介绍

Redisson是一个用于Java的分布式和高可用的Java对象的框架&#xff0c;它基于Redis实现。在Spring Boot应用程序中集成Redisson可以帮助我们更轻松地实现分布式锁、分布式对象、分布式集合等功能。本文将介绍如何在Spring Boot项目中集成Redisson&#xff0c;并展示一些基本用法…

vs正则搜索 int main() 排除 // int main()

1 ctrl f 2 选择正则 3 表达式 ^\s*int\smain\(\) ^ 表示匹配行开始\s* 匹配0个或多个空格int 匹配int关键字\s 匹配一个或多个空格main\( 匹配main函数和左括号

ChatGPT真有很多人在用吗?——回答一位知友的问题

先上结论 是的。数据不会撒谎&#xff0c;用户拿脚投票&#xff0c;ChatGPT发布仅五天内就达到了100万用户&#xff0c;是有史以来增长最快的消费者应用程序。2023年全球前50款AI工具就收获了240亿次访问&#xff0c;其中ChatGPT收获了146亿次访问。 一些想法和思考 我的一些…

9.defer语句调用顺序

目录 概述实践defer结果defer和return执行顺序 结束 概述 defer 类似 java 中的异常处理中的 finally &#xff0c;在 Go 中 defer 是一种压栈出栈操作。 实践 defer package mainimport "fmt"func demo1() {fmt.Println("demo1") }func demo2() {fmt.Pr…

MySql调优(三)Query SQL优化(2)profiler诊断工具

Mysql中自带性能分析工具Profile。注意&#xff1a;profile仅对当前会话有效 一、操作步骤 1、打开 profile set profiling1; 2、执行sql语句 3、分析sql语句执行时间 show profiles 其他参数&#xff1a; ALL&#xff1a;显示所有的开销信息。 BLOCK IO&#xff1a;显示块…

深入理解 Golang 的 crypto/elliptic:椭圆曲线密码学的实践指南

深入理解 Golang 的 crypto/elliptic&#xff1a;椭圆曲线密码学的实践指南 引言crypto/elliptic 库概览基本使用教程高级应用案例性能与安全考量结论 引言 在当今数字时代&#xff0c;数据安全和加密技术成为了信息技术领域的重中之重。特别是在网络通信和数据存储领域&#…

使用Java实现HTTP持久连接:一次与网络的“长聊“

大家都知道&#xff0c;传统的HTTP连接就像是一次性的餐具&#xff0c;每发送一个请求&#xff0c;就得重新建立一个连接&#xff0c;然后快速用完就扔。这对于网络资源来说&#xff0c;简直就是一场"大肆挥霍"的派对。但幸好&#xff0c;我们有HTTP持久连接&#xf…

大力说视频号第三课:手把手教你视频号如何认证

视频号生态不断完善&#xff0c;越来越多的创作者认识到视频号认证的重要性。微信视频号认证不但能提升搜索排名&#xff0c;还能直播推流、与企业微信的关联等优势。 今天大力就来向大家介绍一下视频号如何做认证。 01 个人认证 个人认证又包括兴趣认证和职业认证。 A、兴趣…

华为突然官宣:新版鸿蒙系统,正式发布

华为&#xff0c;一家始终引领科技创新潮流的全球性企业&#xff0c;近日再次引发行业震动——全新HarmonyOS NEXT&#xff0c;被誉为“纯血版鸿蒙”的操作系统正式官宣。这是华为在操作系统领域迈出的坚实且具有突破性的一步&#xff0c;标志着华为正逐步摆脱对安卓生态系统的…

Ajax 详解及其使用

Ajax&#xff08;Asynchronous JavaScript and XML&#xff09;是一种在客户端与服务器之间进行异步通信的技术&#xff0c;它允许网页在不重新加载整个页面的情况下&#xff0c;与服务器交换数据并更新部分网页内容。Ajax 的核心是XMLHttpRequest&#xff08;XHR&#xff09;对…

【异常处理】word或ppt打开后没反应或闪退,或者报错由安全模式打开

折腾了2个小时&#xff0c;可算解决了&#xff0c;办法是在【控制面板】中右击&#xff0c;选择【更改】 选择联机修复&#xff0c;然后耐心等待&#xff0c;最后再打开就没问题了。

Task05:PPO算法

本篇博客是本人参加Datawhale组队学习第五次任务的笔记 【教程地址】https://github.com/datawhalechina/joyrl-book 【强化学习库JoyRL】https://github.com/datawhalechina/joyrl/tree/main 【JoyRL开发周报】 https://datawhale.feishu.cn/docx/OM8fdsNl0o5omoxB5nXcyzsInGe…

消息队列的应用场景

消息队列的应用场景 消息队列中间件是分布式系统中重要的组件&#xff0c;主要解决应用耦合&#xff0c;异步消息&#xff0c;流量削锋等问题实现高性能&#xff0c;高可用&#xff0c;可伸缩和最终一致性架构使用较多的消息队列有ActiveMQ&#xff0c;RabbitMQ&#xff0c;Ze…

vue 发布自己的npm组件

1、在项目任意位置创建index.ts文件 2、导入要到处的组件&#xff0c;使用vue提供的install 功能全局挂在&#xff1b; import GWButton from "/views/GWButton.vue"; import GWAbout from "/views/AboutView.vue";const components {GWButton,GWAbout, …

YOLOv8改进:下采样系列 | 一种新颖的基于 Haar 小波的下采样HWD,有效涨点系列

💡💡💡本文独家改进:HWD的核心思想是应用Haar小波变换来降低特征图的空间分辨率,同时保留尽可能多的信息,与传统的下采样方法相比,有效降低信息不确定性。 💡💡💡使用方法:代替原始网络的conv,下采样过程中尽可能包括更多信息,从而提升检测精度。 收录 YO…

总结了一下中继引擎(can中继器,TCP总机器)开发实际经验

多路数据进行中继的研究 1.数据中继的概念 数据中继是一种数据传输技术&#xff0c;用于在两个通信设备之间提供数字信号的传输。它利用数字信道传输数据信号&#xff0c;可以提供永久性和半永久性连接的数字数据传输信道。 数据中继的主要作用是提高通信质量和可靠性&#xf…