forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimesSequence.cs
47 lines (44 loc) · 1.12 KB
/
PrimesSequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of prime numbers.
/// </para>
/// <para>
/// Wikipedia: https://wikipedia.org/wiki/Prime_number.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000040.
/// </para>
/// </summary>
public class PrimesSequence : ISequence
{
/// <summary>
/// Gets sequence of prime numbers.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
yield return 2;
var primes = new List<BigInteger>
{
2,
};
var n = new BigInteger(3);
while (true)
{
if (primes.All(p => n % p != 0))
{
yield return n;
primes.Add(n);
}
n += 2;
}
}
}
}
}