冗余设计用了长度1000000的列表,如果只考虑20万左右的列表,可以再缩小5倍的时间。package main
import (
"fmt"
"time"
)
//题目6:
//
//前六个质数是 2, 3, 5, 7, 11 和 13,其中第 6 个是 13。
//
//第 10001 个质数是多少?
func main() {
t := time.Now()
var nums [1000000]int
nums[0] = 1
nums[1] = 1
for i := 2; i < 1000; i++ {
var j int
for j = i * i; j < 1000000; j += i {
nums[j] = 1
}
}
var c int = 0
var count int = 0
for c = 2; c < 1000000; c++ {
if nums[c] == 0 {
count++
}
if count == 10001 {
fmt.Println(c)
break
}
}
tt := time.Now()
fmt.Println("耗时:", (tt.Nanosecond()-t.Nanosecond())/1e6, "ms")
}
输出:C:\Users\Anaconda\GolandProjects\EulerProject\output\go_build_Euler06_go.exe
104743
耗时: 19 ms
进程 已完成,退出代码为 0
Go语言还真是快 |