forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfPrimesByNumberOfDigitsSequence.cs
45 lines (42 loc) · 1.23 KB
/
NumberOfPrimesByNumberOfDigitsSequence.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
using System.Collections.Generic;
using System.Numerics;
namespace Algorithms.Sequences
{
/// <summary>
/// <para>
/// Number of primes with n digits
/// (The number of primes between 10^(n-1) and 10^n).
/// </para>
/// <para>
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
/// </para>
/// <para>
/// OEIS: https://oeis.org/A006879.
/// </para>
/// </summary>
public class NumberOfPrimesByNumberOfDigitsSequence : ISequence
{
/// <summary>
/// Gets sequence of number 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;
counter = 0;
powerOf10 *= 10;
}
counter++;
}
}
}
}
}