Last active
December 15, 2015 19:59
-
-
Save alco/5315379 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| m "math" | |
| ) | |
| const LIMIT = 100 | |
| func main() { | |
| // They don't recomend working with raw arrays, so use a slice | |
| prime_bools := make([]bool, LIMIT + 1) | |
| sqrt := int(m.Sqrt(LIMIT)) | |
| // Stay true to the algorithm description | |
| prime_bools[2], prime_bools[3], prime_bools[5] = true, true, true | |
| for i := 1; i <= sqrt; i++ { | |
| for j := 1; j <= sqrt; j++ { | |
| x := uint(i * i) | |
| y := uint(j * j) | |
| var n uint | |
| n = 4*x + y | |
| if (n <= LIMIT) && (n % 12 == 1 || n % 12 == 5) { prime_bools[n] = !prime_bools[n] } | |
| n = 3*x + y | |
| if (n <= LIMIT) && (n % 12 == 7) { prime_bools[n] = !prime_bools[n] } | |
| n = 3*x - y | |
| if (i > j) && (n <= LIMIT) && (n % 12 == 11) { prime_bools[n] = !prime_bools[n] } | |
| } | |
| } | |
| for i := 5; i <= LIMIT; i++ { | |
| q := i * i | |
| if prime_bools[i] { | |
| for k := 1; k*q <= LIMIT; k++ { | |
| prime_bools[k*q] = false | |
| } | |
| } | |
| } | |
| for i, flag := range prime_bools { | |
| if flag { println(i) } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
По ходу так. Только менее очевидно стало. Я б оставил, как есть.