forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfPrimesByPowersOf10Sequence.cs
43 lines (40 loc) · 1.16 KB
/
NumberOfPrimesByPowersOf10Sequence.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
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Sequence of number of primes less than 10^n (with at most n digits).
/// </para>
/// <para>
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A006880.
/// </para>
/// </summary>
public class NumberOfPrimesByPowersOf10Sequence : ISequence
{
/// <summary>
/// Gets sequence of numbers of primes.
/// </summary>
public IEnumerable<BigInteger> Sequence
{
get
{
ISequence primes = new PrimesSequence();
var powerOf10 = new BigInteger(1);
var counter = new BigInteger(0);
foreach (var p in primes.Sequence)
{
if (p > powerOf10)
{
yield return counter;
powerOf10 *= 10;
}
counter++;
}
}
}
}
}