Skip to content

Commit 8629a52

Browse files
OleksiiHesiriak
andauthored
Add A010051 Binary Prime Constant sequence (TheAlgorithms#291)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent 4748ad0 commit 8629a52

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 BinaryPrimeConstantSequenceTests
10+
{
11+
[Test]
12+
public void First10ElementsCorrect()
13+
{
14+
var sequence = new BinaryPrimeConstantSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] { 0, 1, 1, 0, 1, 0, 1, 0, 0, 0 })
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 binary prime constant
9+
/// (Characteristic function of primes: 1 if n is prime, else 0).
10+
/// </para>
11+
/// <para>
12+
/// Wikipedia: https://wikipedia.org/wiki/Prime_constant.
13+
/// </para>
14+
/// <para>
15+
/// OEIS: https://oeis.org/A010051.
16+
/// </para>
17+
/// </summary>
18+
public class BinaryPrimeConstantSequence : ISequence
19+
{
20+
/// <summary>
21+
/// Gets sequence of binary prime constant.
22+
/// </summary>
23+
public IEnumerable<BigInteger> Sequence
24+
{
25+
get
26+
{
27+
ISequence primes = new PrimesSequence();
28+
var n = new BigInteger(0);
29+
30+
foreach (var p in primes.Sequence)
31+
{
32+
for (n++; n < p; n++)
33+
{
34+
yield return 0;
35+
}
36+
37+
yield return 1;
38+
}
39+
}
40+
}
41+
}
42+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ This repository contains algorithms and data structures implemented in C# for ed
111111
* [A006879 Number of Primes by Number of Digits](./Algorithms/Sequences/NumberOfPrimesByNumberOfDigitsSequence.cs)
112112
* [A006880 Number of Primes by Powers of 10](./Algorithms/Sequences/NumberOfPrimesByPowersOf10Sequence.cs)
113113
* [A007318 Binomial](./Algorithms/Sequences/BinomialSequence.cs)
114+
* [A010051 Binary Prime Constant](./Algorithms/Sequences/BinaryPrimeConstantSequence.cs)
114115
* [A011557 Powers of 10](./Algorithms/Sequences/PowersOf10Sequence.cs)
115116
* [A181391 Van Eck's](./Algorithms/Sequences/VanEcksSequence.cs)
116117
* [String](./Algorithms/Strings)

0 commit comments

Comments
 (0)