const max = int(10e5)
// Sieve holds the factors for a given number.
type Sieve map[int][]int
var mysieve = make(Sieve, max)
func genSieve(n int) {
for i := 1; i <= n; i++ {
for j := i; j <= n; j += i {
mysieve[j] = append(mysieve[j], i)
}
}
}
// ListPrimes returns the primes below n.
func ListPrimes(n int) ([]int, error) {
if n > max && n < 1 {
return nil, fmt.Errorf("%d is too high or low, max: %d, min: 1", n, max)
}
genSieve(n)
var primes []int
for i := 2; i < n; i++ {
if len(mysieve[i]) == 2 {
primes = append(primes, i)
}
}
return primes, nil
}
func Primes(limit int) []int {
sieve := make([]bool, limit+1)
out := []int{}
for p := 2; p <= limit; p++ {
if !sieve[p] {
out = append(out, p)
for i := p * p; i <= limit; i += p {
sieve[i] = true
}
}
}
return out
}