Skip to content

Commit 66578c5

Browse files
authored
Add A000720 PrimePi sequence (TheAlgorithms#288)
1 parent 8629a52 commit 66578c5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-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 PrimePiSequenceTests
10+
{
11+
[Test]
12+
public void First10ElementsCorrect()
13+
{
14+
var sequence = new PrimePiSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] { 0, 1, 2, 2, 3, 3, 4, 4, 4, 4 })
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 or equal to n (PrimePi(n)).
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://wikipedia.org/wiki/Prime-counting_function.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A000720.
15+
/// </para>
16+
/// </summary>
17+
public class PrimePiSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of number of primes.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
ISequence primes = new PrimesSequence();
27+
var n = new BigInteger(0);
28+
var counter = new BigInteger(0);
29+
30+
foreach (var p in primes.Sequence)
31+
{
32+
for (n++; n < p; n++)
33+
{
34+
yield return counter;
35+
}
36+
37+
yield return ++counter;
38+
}
39+
}
40+
}
41+
}
42+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ This repository contains algorithms and data structures implemented in C# for ed
105105
* [A000142 Factorial](./Algorithms/Sequences/FactorialSequence.cs)
106106
* [A000290 Squares](./Algorithms/Sequences/SquaresSequence.cs)
107107
* [A000578 Cubes](./Algorithms/Sequences/CubesSequence.cs)
108+
* [A000720 PrimePi](./Algorithms/Sequences/PrimePiSequence.cs)
108109
* [A001462 Golomb's](./Algorithms/Sequences/GolombsSequence.cs)
109110
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)
110111
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)

0 commit comments

Comments
 (0)