当前位置:网站首页>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) } }
边栏推荐
猜你喜欢

tf.reduce_mean() and tf.reduce_sum()

The growth path of a 40W test engineer with an annual salary, which stage are you in?

C语言操作符详解

关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来

2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹

1061 True or False (15 points)

Pico neo3 Unity Packaging Settings

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

Write a resume like this, easy to get the interviewer
随机推荐
Break pad source code compilation--refer to the summary of the big blogger
Decrement operation in tf; tf.assign_sub()
场地预订系统,帮助场馆提高坪效
1.2-误差来源
One-hot in TF
关于架构的认知
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
小目标检测3_注意力机制_Self-Attention
零基础SQL教程: 基础查询 05
C语言操作符详解
2022-08-10 mysql/stonedb-慢SQL-Q16-耗时追踪
The most complete documentation on Excel's implementation of grouped summation
JRS303-Data Verification
Evolution and New Choice of Streaming Structured Data Computing Language
机器学习(一)数据的预处理
Project 1 - PM2.5 Forecast
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
初级软件测试工程师笔试试题,你知道答案吗?
笔试题大疆08.07
Dynamic Agent Learning