1 Star 0 Fork 18

黄峰 / Go

forked from 编程语言算法集 / Go 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
eratosthenesSieve.go 899 Bytes
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
)
//Only works for primes smaller or equal to 10e7
func sieve(upperBound int64) []int64 {
_sieveSize := upperBound + 10
//Creates set to mark wich numbers are primes and wich are not
//true: not primes, false: primes
//this to favor default initialization of arrays in go
var bs [10000010]bool
//creates a slice to save the primes it finds
primes := make([]int64, 0, 1000)
bs[0] = true
bs[1] = true
//iterate over the numbers set
for i := int64(0); i <= _sieveSize; i++ {
//if find one number that is not marked as a compund number, mark all its multiples
if !bs[i] {
for j := i * i; j <= _sieveSize; j += i {
bs[j] = true
}
//Add the prime you just find to the slice of primes
primes = append(primes, i)
}
}
return primes
}
func main() {
//prints first N primes into console
N := 100
primes := sieve(N)
fmt.Println(primes)
}
Go
1
https://gitee.com/huangfeng0618/Go.git
git@gitee.com:huangfeng0618/Go.git
huangfeng0618
Go
Go
master

搜索帮助