forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimePiSequence.cs
42 lines (39 loc) · 1.11 KB
/
PrimePiSequence.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
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of number of primes less than or equal to n (PrimePi(n)).
/// </para>
/// <para>
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A000720.
/// </para>
/// </summary>
public class PrimePiSequence : ISequence
{
/// <summary>
/// Gets sequence of number of primes.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
ISequence primes = new PrimesSequence();
var n = new BigInteger(0);
var counter = new BigInteger(0);
foreach (var p in primes.Sequence)
{
for (n++; n < p; n++)
{
yield return counter;
}
yield return ++counter;
}
}
}
}
}