We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 44b3e09 + 65842de commit 1965bb6Copy full SHA for 1965bb6
example_test.go
@@ -31,12 +31,15 @@ func Example_basics() {
31
// Create the set of all primes less than n in O(n log log n) time.
32
// Try the code with n equal to a few hundred millions and be pleasantly surprised.
33
func Example_primes() {
34
- // Sieve of Eratosthenes
+ // Sieve of Eratosthenes (with optimization suggested by tege)
35
const n = 50
36
sieve := bit.New().AddRange(2, n)
37
+ for k := 4; k < n; k += 2 {
38
+ sieve.Delete(k)
39
+ }
40
sqrtN := int(math.Sqrt(n))
- for p := 2; p <= sqrtN; p = sieve.Next(p) {
- for k := p * p; k < n; k += p {
41
+ for p := 3; p <= sqrtN; p = sieve.Next(p) {
42
+ for k := p * p; k < n; k += 2 * p {
43
sieve.Delete(k)
44
}
45
0 commit comments