-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactor.go
53 lines (49 loc) · 928 Bytes
/
factor.go
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
43
44
45
46
47
48
49
50
51
52
53
package main
/**
* @desc
* 质因数分解go语言示例
*/
import "fmt"
func factorize(num int, len int) []int {
var result = make([]int, len)
var i int = 2
var resultIdx int = 0
for i*i <= num {
for num%i == 0 {
result[resultIdx] = i
num /= i
resultIdx++
}
i++
}
if num > 1 {
result[resultIdx] = num
}
return result
}
func main() {
fmt.Println("Hello, 质因数分解!")
var num int = 120
var len = num / 2
var result []int = factorize(num, len)
for i := 0; i < len; i++ {
if result[i] <= 0 {
break
}
if i > 0 {
fmt.Print(" * ")
}
fmt.Print(result[i])
}
}
/**
jarry@jarrys-MacBook-Pro factor % go version
go version go1.15 darwin/amd64
jarry@jarrys-MacBook-Pro factor % go build factor.go
jarry@jarrys-MacBook-Pro factor % ./factor
Hello, 质因数分解!
2 * 2 * 2 * 3 * 5%
jarry@jarrys-MacBook-Pro factor % go run factor.go
Hello, 质因数分解!
2 * 2 * 2 * 3 * 5%
*/