码迷,mamicode.com
首页 > 其他好文 > 详细

PE-7 10001st prime

时间:2018-12-09 20:39:53      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:imp   div   oat   print   als   code   tmp   pack   ack   

package main

import (
    "fmt"
    "math"
)

func main() {
    i := 2
    num := 1
    for num <= 10001 {
        if Prime(i) {
            num++
        }
        i++
    }
    fmt.Println(i - 1)
}
func Prime(num int) bool {
    if num == 2 || num == 3 {
        return true
    }
    if num%6 != 1 && num%6 != 5 {
        return false
    }
    tmp := int(math.Sqrt(float64(num)))
    for i := 5; i <= tmp; i += 6 {
        if num%i == 0 || num%(i+2) == 0 {
            return false
        }
    }
    return true
}

结果:104743

PE-7 10001st prime

标签:imp   div   oat   print   als   code   tmp   pack   ack   

原文地址:https://www.cnblogs.com/miria-486/p/10093002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!