Skip to content

Commit c78a72b

Browse files
Valdas3Valdas
and
Valdas
authored
Add Recaman's sequence (TheAlgorithms#250)
Co-authored-by: Valdas <[email protected]>
1 parent 6fd8e66 commit c78a72b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 RecamansSequenceTests
10+
{
11+
[Test]
12+
public void First50ElementsCorrect()
13+
{
14+
// Taken from http://oeis.org/A005132
15+
var expected = new BigInteger[]
16+
{
17+
0, 1, 3, 6, 2, 7, 13, 20, 12, 21,
18+
11, 22, 10, 23, 9, 24, 8, 25, 43, 62,
19+
42, 63, 41, 18, 42, 17, 43, 16, 44, 15,
20+
45, 14, 46, 79, 113, 78, 114, 77, 39, 78,
21+
38, 79, 37, 80, 36, 81, 35, 82, 34, 83
22+
};
23+
24+
var sequence = new RecamansSequence().Sequence.Take(50);
25+
26+
sequence.Should().Equal(expected);
27+
}
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Recaman's sequence. a(0) = 0; for n > 0, a(n) = a(n-1) - n if nonnegative and not already in the sequence, otherwise a(n) = a(n-1) + n.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Recam%C3%A1n%27s_sequence.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: http://oeis.org/A005132.
15+
/// </para>
16+
/// </summary>
17+
public class RecamansSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets Recaman's sequence.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
yield return 0;
27+
var elements = new HashSet<BigInteger> { 0 };
28+
var previous = 0;
29+
var i = 1;
30+
31+
while (true)
32+
{
33+
var current = previous - i;
34+
if (current < 0 || elements.Contains(current))
35+
{
36+
current = previous + i;
37+
}
38+
39+
yield return current;
40+
previous = current;
41+
elements.Add(current);
42+
i++;
43+
}
44+
}
45+
}
46+
}
47+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This repository contains algorithms and data structures implemented in C# for ed
9090
* [A000040 Primes](./Algorithms/Sequences/PrimesSequence.cs)
9191
* [A000045 Fibonacci](./Algorithms/Sequences/FibonacciSequence.cs)
9292
* [A000142 Factorial](./Algorithms/Sequences/FactorialSequence.cs)
93+
* [A005132 Recaman's](./Algorithms/Sequences/RecamansSequence.cs)
9394
* [A007318 Binomial](./Algorithms/Sequences/BinomialSequence.cs)
9495
* [String](./Algorithms/Strings)
9596
* [Longest Consecutive Character](./Algorithms/Strings/GeneralStringAlgorithms.cs)

0 commit comments

Comments
 (0)