Skip to content

Commit bcd50b8

Browse files
OleksiiHesiriak
andauthored
Add A000290 Squares Sequence (TheAlgorithms#285)
Co-authored-by: Oleksii Herasymenko <oherasy@softserveinc.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent ed7f9b5 commit bcd50b8

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-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 SquaresSequenceTests
10+
{
11+
[Test]
12+
public void First10ElementsCorrect()
13+
{
14+
var sequence = new SquaresSequence().Sequence.Take(10);
15+
sequence.SequenceEqual(new BigInteger[] { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 })
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Numerics;
3+
4+
namespace Algorithms.Sequences
5+
{
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of square numbers.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://wikipedia.org/wiki/Square_number.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: http://oeis.org/A000290.
15+
/// </para>
16+
/// </summary>
17+
public class SquaresSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of square numbers.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
var n = new BigInteger(0);
27+
28+
while (true)
29+
{
30+
yield return n * n;
31+
n++;
32+
}
33+
}
34+
}
35+
}
36+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ This repository contains algorithms and data structures implemented in C# for ed
102102
* [A000045 Fibonacci](./Algorithms/Sequences/FibonacciSequence.cs)
103103
* [A000108 Catalan](./Algorithms/Sequences/CatalanSequence.cs)
104104
* [A000142 Factorial](./Algorithms/Sequences/FactorialSequence.cs)
105+
* [A000290 Squares](./Algorithms/Sequences/SquaresSequence.cs)
105106
* [A000578 Cubes](./Algorithms/Sequences/CubesSequence.cs)
106107
* [A001462 Golomb's](./Algorithms/Sequences/GolombsSequence.cs)
107108
* [A001478 Negative Integers](./Algorithms/Sequences/NegativeIntegersSequence.cs)

0 commit comments

Comments
 (0)