123456789101112131415161718192021222324252627282930313233343536 |
- package sieve
- import "log"
- const (
- Maybe = iota
- Prime
- NotPrime
- )
- func Sieve(limit int) []int {
- all := make([]int, limit+1)
- all[0] = NotPrime
- all[1] = NotPrime
- for i := 2; i <= limit; i++ {
- all[i] = Maybe
- }
- for i := 2; i <= limit; i++ {
- if all[i] == NotPrime {
- continue
- }
- all[i] = Prime
- for j := 2; j <= limit/i; j++ {
- all[i*j] = NotPrime
- }
- log.Printf("After %3d: %v\n", i, all)
- }
- res := make([]int, 0, len(all))
- for i, v := range all {
- if v == Prime {
- res = append(res, i)
- }
- }
- return res
- }
|