当前位置:网站首页>Getting Started with Kotlin Algorithms Calculating Prime Numbers and Optimization
Getting Started with Kotlin Algorithms Calculating Prime Numbers and Optimization
2022-08-11 08:13:00 【Yi Pangzhou】
class GetprimeNumber { /* * 获取[1,n]区间素数*/ fun forEachNumberGetprime(number: Int): List<Int> { val integers = ArrayList<Int>() for (i in 1 until number) if (isPrimeNumber(2, i)) integers.add(i) return integers } /** * Get the prime numbers from the specified interval */ fun forEachNumberGetprimeToSpecifiedPosition(startPosition: Int, endPosition: Int): List<Int> { val integers = ArrayList<Int>() for (i in startPosition until endPosition) if (isPrimeNumber(2, i)) integers.add(i) return integers } /** * 打印从1到N的素数 */ fun forEachPrintNumberGetprime(number: Int) { for (i in 1 until number) if (isPrimeNumber(2, i)) println("number = [$i]是素数") } /** * Print the prime numbers from the specified interval */ fun forEachPrintNumberGetprimeToSpecifiedPosition(startPosition: Int, endPosition: Int) { for (i in startPosition until endPosition) if (isPrimeNumber(2, i)) println("number = [$i]是素数") } /** * 因为不管怎么计算由于非素数数都可以通过1·9中通过乘计算得出所以除了1和2只需要继续是否可以被2-9整除就可以 * 这一说法利用了提取最小公因式来计算得出 * 当然要避免一个重要问题就是当它是个位数字的时候也就是1 、 2 、 3 、 5 、7so return directly20以内素数 * 这样计算的好处在于避免了传统递归从1到n的反复计算更加高效的计算出素数面对千位以上的数据使用 * 也避免了过多使用这一算法(冗余重复性计算)的:判断素数的方法:用一个数分别去除2到sqrt(这个数的平方根),如果能被整除, 则表明此数不是素数,反之是素数这一种算法更加快捷 * 避免了重复计算的冗余 */ fun isPrimeNumber(divisor: Int, number: Int): Boolean { if (number % divisor == 0) return false else 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) return false else if (divisor == 9) { return isPrimeNumber(11, divisor) } else if (divisor > 9) { return if (divisor < Math.sqrt(number.toDouble())) { isPrimeNumber(divisor + 1, number) } else if (divisor.toDouble() == Math.sqrt(number.toDouble())) false else true } return isPrimeNumber(divisor + 1, number) } }
边栏推荐
- 剑指offer专项突击版第26天
- 链式编程注解
- 9、Neural Sparse Voxel Fields
- LoRa芯片的特征
- 【Day_13 0509】▲跳石板
- 查询跟踪快递单号物流,智能分析物流中转有延误的单号
- About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
- AUTOSAR从入门到精通番外篇(八)-C语言常用技巧50个
- The softmax function is used in TF;
- 2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
猜你喜欢

Pico neo3 Unity Packaging Settings
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子

3.2 - classification - Logistic regression

软件测试常用工具的用途及优缺点比较(详细)

golang 字符串操作

CSDN21天学习挑战赛——封装(06)

机器学习(一)数据的预处理

2022 China Soft Drink Market Insights

1036 Programming with Obama (15 points)

My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
随机推荐
对比学习系列(三)-----SimCLR
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
如何通过 IDEA 数据库管理工具连接 TDengine?
【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
Machine Learning Summary (2)
2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
Alibaba Sentinel - Slot chain解析
Decrement operation in tf; tf.assign_sub()
1036 Programming with Obama (15 points)
查找最新人员工资和上上次人员工资的变动情况
Find the latest staff salary and the last staff salary changes
优炫数据库支持多列分区吗?
法律顾问成了律所鸡肋产品了吗?
1081 Check Password (15 points)
软件测试常用工具的用途及优缺点比较(详细)
【Day_13 0509】▲跳石板
1.2 - error sources
nodejs微服务中跨域,请求,接口,参数拦截等功能
1106 2019 Sequence (15 points)
4.1 - Support Vector Machines