Last active
April 14, 2018 04:48
-
-
Save gushwell/61c79e3b3d35b402828e2bf7609b6872 to your computer and use it in GitHub Desktop.
C#:エラトステネスの篩を使って素数を求める ref: https://qiita.com/gushwell/items/5d3988a2f5de0587c88f
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
static IEnumerable<int> Primes(int maxnum) { | |
// var sieve = new BitArray(maxnum + 1, true); | |
bool[] sieve = Enumerable.Repeat(true, maxnum + 1).ToArray(); | |
int squareroot = (int)Math.Sqrt(maxnum); | |
for (int i = 2; i <= squareroot; i++) { | |
if (sieve[i] == false) | |
continue; | |
for (int n = i * 2; n <= maxnum; n += i) | |
sieve[n] = false; | |
} | |
for (int i = 2; i <= maxnum; i++) | |
if (sieve[i] == true) | |
yield return i; | |
} |
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
static IEnumerable<int> Primes(int maxnum) { | |
var sieve = new BitArray(maxnum + 1, true); | |
int squareroot = (int)Math.Sqrt(maxnum); | |
for (int i = 2; i <= squareroot; i++) { | |
if (sieve[i] == false) | |
continue; | |
for (int n = i * 2; n <= maxnum; n += i) | |
sieve[n] = false; | |
if (sieve[i]) | |
yield return i; // ここで列挙してしまう | |
} | |
for (int i = squareroot + 1; i <= maxnum; i++) | |
if (sieve[i] == true) | |
yield return i; | |
} |
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
static IEnumerable<int> Primes(int maxnum) { | |
yield return 2; | |
yield return 3; | |
var sieve = new BitArray(maxnum + 1); | |
int squareroot = (int)Math.Sqrt(maxnum); | |
for (int i = 2; i <= squareroot; i++) { | |
if (sieve[i] == false) { | |
for (int n = i * 2; n <= maxnum; n += i) | |
sieve[n] = true; | |
} | |
for (int n = i * i + 1; n <= maxnum && n < (i + 1) * (i + 1); n++) { | |
if (!sieve[n]) | |
yield return n; | |
} | |
} | |
} | |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Gushwell.Etude { | |
class Program { | |
static void Main(string[] args) { | |
foreach(var n in Primes(200)) | |
Console.Write("{0,3} ",n); | |
Console.WriteLine(); | |
Console.ReadLine(); | |
} | |
static IEnumerable<int> Primes(int maxnum) { | |
int[] sieve = Enumerable.Range(0, maxnum + 1).ToArray(); | |
sieve[1] = 0; // 0 : 素数ではない | |
int squareroot = (int)Math.Sqrt(maxnum); | |
for (int i = 2; i <= squareroot; i++) { | |
if (sieve[i] <= 0) | |
continue; | |
for (int n = i * 2; n <= maxnum; n += i) | |
sieve[n] = 0; | |
} | |
return sieve.Where(n => n > 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment