Skip to content

Commit d73ae5f

Browse files
OleksiiHesiriak
andauthored
Add A006880 Number of primes sequence (TheAlgorithms#284)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent bcd50b8 commit d73ae5f

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-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 NumberOfPrimesByPowersOf10SequenceTests
10+
{
11+
[Test]
12+
public void First5ElementsCorrect()
13+
{
14+
var sequence = new NumberOfPrimesByPowersOf10Sequence().Sequence.Take(5);
15+
sequence.SequenceEqual(new BigInteger[] { 0, 4, 25, 168, 1229 })
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of number of primes less than 10^n (with at most n digits).
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A006880.
15+
/// </para>
16+
/// </summary>
17+
public class NumberOfPrimesByPowersOf10Sequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of numbers of primes.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
ISequence primes = new PrimesSequence();
27+
var powerOf10 = new BigInteger(1);
28+
var counter = new BigInteger(0);
29+
30+
foreach (var p in primes.Sequence)
31+
{
32+
if (p > powerOf10)
33+
{
34+
yield return counter;
35+
powerOf10 *= 10;
36+
}
37+
38+
counter++;
39+
}
40+
}
41+
}
42+
}
43+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ This repository contains algorithms and data structures implemented in C# for ed
107107
* [A001462 Golomb's](./Algorithms/Sequences/GolombsSequence.cs)
108108
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)
109109
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)
110+
* [A006880 Number of Primes by Powers of 10](./Algorithms/Sequences/NumberOfPrimesByPowersOf10Sequence.cs)
110111
* [A007318 Binomial](./Algorithms/Sequences/BinomialSequence.cs)
111112
* [A181391 Van Eck's](./Algorithms/Sequences/VanEcksSequence.cs)
112113
* [String](./Algorithms/Strings)

0 commit comments

Comments
 (0)