当前位置:网站首页>Kotlin算法入门求完全数
Kotlin算法入门求完全数
2022-08-11 08:01:00 【易庞宙】
/*一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。*/ class CompleteNumber { private var firstFactorNumber: Int = 0 /** * 因为不管怎么计算由于非素数数都可以通过1·9中通过乘计算得出所以除了1和2只需要继续是否可以被2-9整除就可以 * 这一说法利用了提取最小公因式来计算得出 * 当然要避免一个重要问题就是当它是个位数字的时候也就是1 、 2 、 3 、 5 、7的时候直接返回 * 这样计算的好处在于避免了传统递归从1到n的反复计算更加高效的计算出素数面对千位以上的数据使用 * 也避免了过多使用这一算法(冗余重复性计算)的: * 判断素数的方法:用一个数分别去除2到sqrt(这个数的平方根),如果能被整除, 则表明此数不是素数,反之是素数这一种算法更加快捷 * 避免了重复计算的冗余 */ fun isPrimeNumber(divisor: Int, number: Int): Boolean { if (number % divisor == 0) { firstFactorNumber = divisor return false } if (number == 1 || number == 2 || number == 3 || number == 5 || number == 7 || number == 11 || number == 13 || number == 17 || number == 19) return true else if (number <= 20) { firstFactorNumber = 1 return false } else if (divisor == 9) { return isPrimeNumber(11, divisor) } else if (divisor > 9) { if (divisor < Math.sqrt(number.toDouble())) { return isPrimeNumber(divisor + 1, number) } else if (divisor.toDouble() == Math.sqrt(number.toDouble())) { firstFactorNumber = divisor return false } else return true } return isPrimeNumber(divisor + 1, number) } /*调用优化算法*/ fun isCompleteNumber(number: Int) { if (number < 6) { println("从1到" + number + "没有完全数") } else { for (i in 6..number) { /*素数不可能是完全数所以可以计算直接跳过 * 每一次计算判断素数的时候如果不是通过计算 * 是否为素数出的第一个因数+1为起始点进行计算避免起点重复计算*/ if (isPrimeNumber(2, number)) continue var sum = 0 if (firstFactorNumber > 1) sum += 1 + firstFactorNumber else if (firstFactorNumber == 1) sum += 1 for (j in firstFactorNumber + 1 until i) { if (i % j == 0) { sum = sum + j } } if (sum == i) { println(i) } } } } /*传统做法*/ fun tradition() { var s: Int for (i in 1..1000000) { s = 0 for (j in 1 until i) if (i % j == 0) s = s + j if (s == i) print(i.toString() + " ") } println() } }
边栏推荐
- Dynamic Agent Learning
- 1081 Check Password (15 points)
- 1076 Wifi密码 (15 分)
- One-hot in TF
- Interaction of Pico neo3 in Unity
- There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
- 3.1-分类-概率生成模型
- JUC并发编程
- 4.1ROS运行管理/launch文件
- 我的创作纪念日丨感恩这365天来有你相伴,不忘初心,各自精彩
猜你喜欢
JRS303-Data Verification
1.2 - error sources
1106 2019 Sequence (15 points)
The softmax function is used in TF;
Active users of mobile banking grew rapidly in June, hitting a half-year high
3.2 - classification - Logistic regression
Keep track of your monthly income and expenses through bookkeeping
1096 大美数 (15 分)
1.1-回归
1061 True or False (15 points)
随机推荐
IQUNIX A80 exploring TTC金粉 初体验
Project 1 - PM2.5 Forecast
迷你图书馆系统(对象+数组)
Item 2 - Annual Income Judgment
Pico neo3 Unity Packaging Settings
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
Keep track of your monthly income and expenses through bookkeeping
opengauss创建用户权限问题
1081 Check Password (15 points)
The most complete documentation on Excel's implementation of grouped summation
1101 How many times B is A (15 points)
分布式锁-Redission - 缓存一致性解决
2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
无服务器+域名也能搭建个人博客?真的,而且很快
关于架构的认知
1051 Multiplication of Complex Numbers (15 points)
1.1-回归
项目1-PM2.5预测
1076 Wifi Password (15 points)
Active users of mobile banking grew rapidly in June, hitting a half-year high