|
发表于 2022-2-22 22:05:39
|
显示全部楼层
- package main
- import (
- "fmt"
- "math"
- "time"
- )
- //题目3:
- //
- //13195 的质数因子有 5, 7, 13 和 29。
- //
- //600851475143 的最大质数因子是多少?
- func main() {
- t := time.Now()
- limit := int64(math.Sqrt(600851475143))
- var remain int64 = 600851475143
- var max int64 = 2
- var i int64
- fmt.Println("分解质因数:")
- for i = 2; i < limit; i++ {
- if remain%i == 0 {
- remain /= i
- fmt.Println(i)
- if i > max {
- max = i
- }
- i = 2
- }
- }
- if remain > max {
- max = remain
- }
- fmt.Println("最大质因数是: ", max)
- tt := time.Now()
- fmt.Println("耗时:", (tt.Nanosecond()-t.Nanosecond())/1e6, "ms")
- }
复制代码
输出:
- GOROOT=C:\Program Files\Go #gosetup
- GOPATH=C:\Users\Administrator\go #gosetup
- "C:\Program Files\Go\bin\go.exe" build -o C:\Users\Administrator\AppData\Local\Temp\GoLand\___2go_build_GoProject_src.exe C:\Users\Administrator\Documents\GoProject\src\Euler03.go #gosetup
- C:\Users\Administrator\AppData\Local\Temp\GoLand\___2go_build_GoProject_src.exe
- 分解质因数:
- 71
- 839
- 1471
- 6857
- 最大质因数是: 6857
- 耗时: 2 ms
- 进程 已完成,退出代码为 0
复制代码
Go语言果然运行速度比Python快非常多 |
|