Skip to content

Commit 4748ad0

Browse files
authored
Add A006879 Number of Primes by Number of Digits sequence (TheAlgorithms#287)
1 parent 681ca77 commit 4748ad0

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Linq;
2+
using System.Numerics;
3+
using Algorithms.Sequences;
4+
using FluentAssertions;
5+
using NUnit.Framework;
6+
7+
namespace Algorithms.Tests.Sequences
8+
{
9+
public class NumberOfPrimesByNumberOfDigitsSequenceTests
10+
{
11+
[Test]
12+
public void First5ElementsCorrect()
13+
{
14+
var sequence = new NumberOfPrimesByNumberOfDigitsSequence().Sequence.Take(5);
15+
sequence.SequenceEqual(new BigInteger[] { 0, 4, 21, 143, 1061 })
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Number of primes with n digits
9+
/// (The number of primes between 10^(n-1) and 10^n).
10+
/// </para>
11+
/// <para>
12+
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
13+
/// </para>
14+
/// <para>
15+
/// OEIS: https://oeis.org/A006879.
16+
/// </para>
17+
/// </summary>
18+
public class NumberOfPrimesByNumberOfDigitsSequence : ISequence
19+
{
20+
/// <summary>
21+
/// Gets sequence of number of primes.
22+
/// </summary>
23+
public IEnumerable<BigInteger> Sequence
24+
{
25+
get
26+
{
27+
ISequence primes = new PrimesSequence();
28+
var powerOf10 = new BigInteger(1);
29+
var counter = new BigInteger(0);
30+
31+
foreach (var p in primes.Sequence)
32+
{
33+
if (p > powerOf10)
34+
{
35+
yield return counter;
36+
counter = 0;
37+
powerOf10 *= 10;
38+
}
39+
40+
counter++;
41+
}
42+
}
43+
}
44+
}
45+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ This repository contains algorithms and data structures implemented in C# for ed
108108
* [A001462 Golomb's](./Algorithms/Sequences/GolombsSequence.cs)
109109
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)
110110
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)
111+
* [A006879 Number of Primes by Number of Digits](./Algorithms/Sequences/NumberOfPrimesByNumberOfDigitsSequence.cs)
111112
* [A006880 Number of Primes by Powers of 10](./Algorithms/Sequences/NumberOfPrimesByPowersOf10Sequence.cs)
112113
* [A007318 Binomial](./Algorithms/Sequences/BinomialSequence.cs)
113114
* [A011557 Powers of 10](./Algorithms/Sequences/PowersOf10Sequence.cs)

0 commit comments

Comments
 (0)