Skip to content

Commit 43e1caf

Browse files
Add Sequence A002717 (TheAlgorithms#354)
1 parent 0c516ee commit 43e1caf

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 static class MatchstickTriangleSequenceTests
11+
{
12+
private static BigInteger[] TestList = {
13+
0, 1, 5, 13, 27, 48, 78, 118, 170, 235, 315, 411, 525, 658,
14+
812, 988, 1188, 1413, 1665, 1945, 2255, 2596, 2970, 3378,
15+
3822, 4303, 4823, 5383, 5985, 6630, 7320, 8056, 8840, 9673,
16+
10557, 11493, 12483, 13528, 14630, 15790, 17010, 18291,
17+
19635, 21043, 22517,
18+
};
19+
/// <summary>
20+
/// This test uses the list values provided from http://oeis.org/A002717/list.
21+
/// </summary>
22+
[Test]
23+
public static void TestOeisList()
24+
{
25+
var sequence = new MatchstickTriangleSequence().Sequence.Take(TestList.Length);
26+
sequence.SequenceEqual(TestList).Should().BeTrue();
27+
}
28+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
5+
namespace Algorithms.Sequences;
6+
7+
/// <summary>
8+
/// <para>
9+
/// Sequence of number of triangles in triangular matchstick arrangement of side n for n>=0.
10+
/// </para>
11+
/// <para>
12+
/// M. E. Larsen, The eternal triangle – a history of a counting problem, College Math. J., 20 (1989), 370-392.
13+
/// https://web.math.ku.dk/~mel/mel.pdf.
14+
/// </para>
15+
/// <para>
16+
/// OEIS: http://oeis.org/A002717.
17+
/// </para>
18+
/// </summary>
19+
public class MatchstickTriangleSequence : ISequence
20+
{
21+
/// <summary>
22+
/// <para>
23+
/// Gets number of triangles contained in an triangular arrangement of matchsticks of side length n.
24+
/// </para>
25+
/// <para>
26+
/// This also counts the subset of smaller triangles contained within the arrangement.
27+
/// </para>
28+
/// <para>
29+
/// Based on the PDF referenced above, the sequence is derived from step 8, using the resulting equation
30+
/// of f(n) = (n(n+2)(2n+1) -(delta)(n)) / 8. Using BigInteger values, we can effectively remove
31+
/// (delta)(n) from the previous by using integer division instead.
32+
/// </para>
33+
/// <para>
34+
/// Examples follow.
35+
/// <pre>
36+
/// .
37+
/// / \ This contains 1 triangle of size 1.
38+
/// .---.
39+
///
40+
/// .
41+
/// / \ This contains 4 triangles of size 1.
42+
/// .---. This contains 1 triangle of size 2.
43+
/// / \ / \ This contains 5 triangles total.
44+
/// .---.---.
45+
///
46+
/// .
47+
/// / \ This contains 9 triangles of size 1.
48+
/// .---. This contains 3 triangles of size 2.
49+
/// / \ / \ This contains 1 triangles of size 3.
50+
/// .---.---.
51+
/// / \ / \ / \ This contains 13 triangles total.
52+
/// .---.---.---.
53+
/// </pre>
54+
/// </para>
55+
/// </summary>
56+
public IEnumerable<BigInteger> Sequence
57+
{
58+
get
59+
{
60+
var index = BigInteger.Zero;
61+
var eight = new BigInteger(8);
62+
while (true)
63+
{
64+
var temp = index * (index + 2) * (index * 2 + 1);
65+
var result = BigInteger.Divide(temp, eight);
66+
yield return result;
67+
index++;
68+
}
69+
}
70+
}
71+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ find more than one implementation for the same objective but using different alg
144144
* [A001462 Golomb's](./Algorithms/Sequences/GolombsSequence.cs)
145145
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)
146146
* [A002110 Primorial Numbers](./Algorithms/Sequences/PrimorialNumbersSequence.cs)
147+
* [A002717 Matchstick Triangle Arrangement](./Algorithms/Sequences/MatchstickTriangleSequence.cs)
147148
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)
148149
* [A006577 Number of '3n+1' steps to reach 1](./Algorithms/Sequences/ThreeNPlusOneStepsSequence.cs)
149150
* [A006862 Euclid Numbers](./Algorithms/Sequences/EuclidNumbersSequence.cs)

0 commit comments

Comments
 (0)