当前位置:网站首页>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) } }
边栏推荐
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 4.1 - Support Vector Machines
- About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
- Two state forms of Service
- 兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
- CSDN21天学习挑战赛——封装(06)
- 美术2.4 UV原理基础
- 1002 Write the number (20 points)
- 优炫数据库支持多列分区吗?
- nodejs微服务中跨域,请求,接口,参数拦截等功能
猜你喜欢
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
XXL-JOB 分布式任务调度中心搭建
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
JUC并发编程
Find the latest staff salary and the last staff salary changes
小目标检测3_注意力机制_Self-Attention
Mysql JSON对象和JSON数组查询
基于微信小程序的租房小程序
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
随机推荐
One-hot in TF
Project 1 - PM2.5 Forecast
一根网线两台电脑传输文件
Do you know the basic process and use case design method of interface testing?
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
2022年值得关注的NFT发展趋势
【C语言】每日一题,求水仙花数,求变种水仙花数
Filesystem Hierarchy Standard
4.1 - Support Vector Machines
1051 Multiplication of Complex Numbers (15 points)
1.1-Regression
【Day_13 0509】▲跳石板
1076 Wifi Password (15 points)
RestTemplate工具类
Kaldi语音识别工具编译问题记录(踩坑记录)
Hibernate 的 Session 缓存相关操作
oracle数据库中列转行,列会有变化
1091 N-Defensive Number (15 points)
《剑指offer》题解——week3(持续更新)
AcWing 272. 最长公共上升子序列