-
Notifications
You must be signed in to change notification settings - Fork 2
/
factor.kt
42 lines (39 loc) · 1.01 KB
/
factor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @desc
* 质因数分解kotlin语言示例
*/
fun facorize(number: Int): List<Int> {
val result = ArrayList<Int>();
var i = 2;
var num = number;
while (i * i <= num) {
while (num % i == 0) {
num = num / i;
result.add(i);
}
i++;
}
if (num > 1) {
result.add(num);
}
return result;
}
fun main() {
var num: Int = 20;
println("prime factorize:");
val result = facorize(num);
for (i in result.indices) {
// println("resule[${i}] = ${result[i]} and result.size = ${result.size}");
print(result[i]);
if (i < result.size - 1) {
print(" * ");
}
}
}
/**
jarry@jarrys-MacBook-Pro factor % kotlinc factor.kt -include-runtime -d factor.jar
Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
jarry@jarrys-MacBook-Pro factor % java -jar factor.jar
prime factorize:
2 * 2 * 5%
*/