

[Golang] Sieve of Eratosthenes
source link: http://siongui.github.io/2017/04/17/go-sieve-of-eratosthenes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] Sieve of Eratosthenes
April 17, 2017
Go implementation of Sieve of Eratosthenes. See from GeeksforGeeks [1]. The Go code is ported from the C/C++ code of GeeksforGeeks.
package main import ( "fmt" ) func SieveOfEratosthenes(n int) []int { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. integers := make([]bool, n+1) for i := 2; i < n+1; i++ { integers[i] = true } for p := 2; p*p <= n; p++ { // If integers[p] is not changed, then it is a prime if integers[p] == true { // Update all multiples of p for i := p * 2; i <= n; i += p { integers[i] = false } } } // return all prime numbers <= n var primes []int for p := 2; p <= n; p++ { if integers[p] == true { primes = append(primes, p) } } return primes } func main() { fmt.Println(SieveOfEratosthenes(20)) fmt.Println(SieveOfEratosthenes(30)) fmt.Println(SieveOfEratosthenes(40)) }
Recommend
-
38
Lately, on invitation of my right honourable friend Michal , I've been trying to solve some problems from the Euler project and felt the need t...
-
14
-
9
RAGANWALD.COM Implementing the Sieve of Eratosthenes with Functional Programming Programming interviews often include a “Fizzbuzz” test,...
-
6
The sieve of Eratosthenes finds all prime numbers up to a given limit. Method The algorithm st...
-
11
In this article, we will learn to implement the Sieve of Eratosthenes using C++. If you’re a competitive programmer, most probably you’d be familiar with this term. But if you’re not, sit back and relax, we’ll see in the following sections wh...
-
9
Large Prime Check Using The Sieve Of Eratosthenes in C++ Filed Under: C++In this article, we will learn...
-
13
Prime Factorization Using The Sieve Of Eratosthenes in C++ Filed Under: JavaToday we will learn prime factoriz...
-
7
[JavaScript] Sieve of Eratosthenes April 28, 2018 JavaScript implementation...
-
8
Online Sieve of Eratosthenes Demo via Go and Vue.js May 01, 2018 Onli...
-
21
[Vue.js] Online Sieve of Eratosthenes Demo May 02, 2018
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK