当前位置:网站首页>Getting Started with Kotlin Algorithms Calculating Prime Factors
Getting Started with Kotlin Algorithms Calculating Prime Factors
2022-08-11 08:12:00 【Yi Pangzhou】
/* 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数. 比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. 现在,你的程序要读入一个[2,100000]范围内的整数,然后输出它的质因数分解式;当读到的就是素数时,输出它本身 */ class QualityFactor { /** * Because no matter how you calculate it, it can pass because of non-prime numbers1·9is calculated by multiplication, so except1和2Just go ahead and see if it can be2-9整除就可以 * This statement is calculated by extracting the least common factor * Of course an important problem to avoid is when it's a single digit number1 、 2 、 3 、 5 、7的时候直接返回 * The advantage of this calculation is that it avoids traditional recursive slaves1到nThe repeated calculation is more efficient to calculate the prime number in the face of data with more than one thousand digits * Excessive use of this algorithm is also avoided(Redundant repetitive calculations)的: * 判断素数的方法:用一个数分别去除2到sqrt(这个数的平方根),如果能被整除, 则表明此数不是素数,Conversely, the prime number algorithm is faster * The redundancy of repeated calculation is avoided */ 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) } fun getQualityFactor(number: Int) { var number = number var out = number.toString() + "=" if (isPrimeNumber(2, number)) out = out + number else { while (number != 1) { for (j in 2..number) { /*如果每一次Number都能整除j则让Number/=j*/ if (number % j == 0) { number /= j /*After the division is completed, it is determined whether it is a prime number, so as to avoid a relatively large prime number remaining at the end and then need to perform repeated calculations*/ if (number != 1) { out += j.toString() + "x" if (isPrimeNumber(2, number)) { out += number number = 1 } } else out += j break } } } } println(out) } }
边栏推荐
- Break pad source code compilation--refer to the summary of the big blogger
- Kotlin算法入门兔子数量优化及拓展
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
- Pico neo3 Unity Packaging Settings
- 1106 2019 Sequence (15 points)
- About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
- 1.1-回归
- matrix multiplication in tf
- 欧拉函数(用欧拉筛法求欧拉函数)
猜你喜欢
随机推荐
囍楽cloud task source code
Serverless + domain name can also build a personal blog? Really, and soon
【LeetCode】Summary of linked list problems
用 Antlr 重构脚本解释器
klayout--导出版图为gds文件
美术2.4 UV原理基础
C Primer Plus(6) 中文版 第1章 初识C语言 1.6 语言标准
pyqt5实现仪表盘
Kotlin算法入门计算素数以及优化
golang 字符串操作
Active users of mobile banking grew rapidly in June, hitting a half-year high
2.1 - Gradient Descent
JUC并发编程
Keep track of your monthly income and expenses through bookkeeping
2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
2.1-梯度下降
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子
软件测试常用工具的用途及优缺点比较(详细)
JRS303-数据校验
1071 Small Gamble (15 points)