Skip to content

Commit 35a304b

Browse files
Add Sequence A000292 (TheAlgorithms#355)
1 parent 43e1caf commit 35a304b

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
[TestFixture]
10+
public class TetrahedralSequenceTests
11+
{
12+
private static readonly BigInteger[] TestList = {
13+
0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455,
14+
560, 680, 816, 969, 1140, 1330, 1540, 1771, 2024, 2300,
15+
2600, 2925, 3276, 3654, 4060, 4495, 4960, 5456, 5984, 6545,
16+
7140, 7770, 8436, 9139, 9880, 10660, 11480, 12341, 13244,
17+
14190, 15180,
18+
};
19+
20+
/// <summary>
21+
/// This test uses the list values provided from http://oeis.org/A000292/list.
22+
/// </summary>
23+
[Test]
24+
public void TestOeisList()
25+
{
26+
var sequence = new TetrahedralSequence().Sequence.Take(TestList.Length);
27+
sequence.SequenceEqual(TestList).Should().BeTrue();
28+
29+
}
30+
}
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 tetrahedral (triangular pyramids) counts for n >= 0.
9+
/// </para>
10+
/// <para>
11+
/// OEIS: http://oeis.org/A000292.
12+
/// </para>
13+
/// <para>
14+
/// Wikipedia: https://en.wikipedia.org/wiki/Tetrahedral_number.
15+
/// </para>
16+
/// </summary>
17+
public class TetrahedralSequence : ISequence
18+
{
19+
/// <summary>
20+
/// <para>
21+
/// Gets the value of packing spheres in a regular tetrahedron
22+
/// with increasing by 1 triangular numbers under each layer.
23+
/// </para>
24+
/// <para>
25+
/// It can be reviewed by starting at the 4th row of Pascal's Triangle
26+
/// following the diagonal values going into the triangle.
27+
/// </para>
28+
/// </summary>
29+
public IEnumerable<BigInteger> Sequence
30+
{
31+
get
32+
{
33+
var index = BigInteger.Zero;
34+
var six = new BigInteger(6);
35+
while (true)
36+
{
37+
yield return BigInteger.Divide(index * (index + 1) * (index + 2), six);
38+
index++;
39+
}
40+
}
41+
}
42+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ find more than one implementation for the same objective but using different alg
138138
* [A000142 Factorial](./Algorithms/Sequences/FactorialSequence.cs)
139139
* [A000215 Fermat Numbers](./Algorithms/Sequences/FermatNumbersSequence.cs)
140140
* [A000290 Squares](./Algorithms/Sequences/SquaresSequence.cs)
141+
* [A000292 Tetrahedral numbers](./Algorithms/Sequences/TetrahedralSequence.cs)
141142
* [A000578 Cubes](./Algorithms/Sequences/CubesSequence.cs)
142143
* [A000720 PrimePi](./Algorithms/Sequences/PrimePiSequence.cs)
143144
* [A001146 Number of Boolean Functions](./Algorithms/Sequences/NumberOfBooleanFunctionsSequence.cs)

0 commit comments

Comments
 (0)