Created
July 7, 2019 07:32
-
-
Save youknowcast/4263b376726a59895bea83c587008f6f to your computer and use it in GitHub Desktop.
eratosthenes with golang
This file contains 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 ( | |
"fmt" | |
"math" | |
) | |
const NUM = 100000 | |
func main() { | |
prime := make([]int, NUM+1) | |
var i,j,limit int | |
for i = 2; i < NUM-1; i++ { | |
prime[i] = 1 | |
} | |
limit = int(math.Sqrt(float64(NUM))) | |
for i=2; i <= limit; i++ { | |
if prime[i] == 1 { | |
for j=2*i; j <=NUM; j++ { | |
if j % i == 0 { | |
prime[j] = 0 | |
} | |
} | |
} | |
} | |
for i=2; i <=NUM; i++ { | |
if prime[i] == 1 { | |
fmt.Print("\n") | |
fmt.Print(i) | |
} | |
} | |
fmt.Print("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment