- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathPrimesSequence.cs
46 lines (42 loc) · 988 Bytes
/
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
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Numerics;
namespaceAlgorithms.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>
publicclassPrimesSequence:ISequence
{
/// <summary>
/// Gets sequence of prime numbers.
/// </summary>
publicIEnumerable<BigInteger>Sequence
{
get
{
yieldreturn2;
varprimes=newList<BigInteger>
{
2,
};
varn=newBigInteger(3);
while(true)
{
if(primes.All(p =>n%p!=0))
{
yieldreturnn;
primes.Add(n);
}
n+=2;
}
}
}
}