Skip to content

Commit cba779c

Browse files
authored
Add A006577 sequence: number of '3n+1' steps to reach 1 (#302)
1 parent da10a33 commit cba779c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
public class ThreeNPlusOneStepsSequenceTests {
9+
[Test]
10+
public void First50ElementsCorrect() {
11+
var sequence = new ThreeNPlusOneStepsSequence().Sequence.Take(50);
12+
var first50 = new BigInteger[] {
13+
0, 1, 7, 2, 5, 8, 16, 3, 19, 6,
14+
14, 9, 9, 17, 17, 4, 12, 20, 20, 7,
15+
7, 15, 15, 10, 23, 10, 111, 18, 18, 18,
16+
106, 5, 26, 13, 13, 21, 21, 21, 34, 8,
17+
109, 8, 29, 16, 16, 16, 104, 11, 24, 24
18+
};
19+
sequence.SequenceEqual(first50).Should().BeTrue();
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Number of halving and tripling steps to reach 1 in the '3n+1' problem.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Collatz_conjecture.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A006577.
15+
/// </para>
16+
/// </summary>
17+
public class ThreeNPlusOneStepsSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of number of halving and tripling steps to reach 1 in the '3n+1' problem.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
BigInteger startingValue = 1;
27+
28+
while (true)
29+
{
30+
BigInteger counter = 0;
31+
BigInteger currentValue = startingValue;
32+
33+
while (currentValue != 1)
34+
{
35+
if (currentValue.IsEven)
36+
{
37+
currentValue /= 2;
38+
}
39+
else
40+
{
41+
currentValue = 3 * currentValue + 1;
42+
}
43+
44+
counter++;
45+
}
46+
47+
yield return counter;
48+
startingValue++;
49+
}
50+
}
51+
}
52+
}
53+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ This repository contains algorithms and data structures implemented in C# for ed
115115
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)
116116
* [A002110 Primorial Numbers](./Algorithms/Sequences/PrimorialNumbersSequence.cs)
117117
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)
118+
* [A006577 Number of '3n+1' steps to reach 1](./Algorithms/Sequences/ThreeNPlusOneStepsSequence.cs)
118119
* [A006862 Euclid Numbers](./Algorithms/Sequences/EuclidNumbersSequence.cs)
119120
* [A006879 Number of Primes by Number of Digits](./Algorithms/Sequences/NumberOfPrimesByNumberOfDigitsSequence.cs)
120121
* [A006880 Number of Primes by Powers of 10](./Algorithms/Sequences/NumberOfPrimesByPowersOf10Sequence.cs)

0 commit comments

Comments
 (0)