Skip to content

Commit b5b0044

Browse files
authored
Add A000215 Fermat Numbers sequence (TheAlgorithms#298)
1 parent e4e4cd3 commit b5b0044

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 FermatNumbersSequenceTests
10+
{
11+
[Test]
12+
public void First5ElementsCorrect()
13+
{
14+
var sequence = new FermatNumbersSequence().Sequence.Take(5);
15+
sequence.SequenceEqual(new BigInteger[] { 3, 5, 17, 257, 65537 })
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 Fermat numbers: a(n) = 2^(2^n) + 1.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://wikipedia.org/wiki/Fermat_number.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A000215.
15+
/// </para>
16+
/// </summary>
17+
public class FermatNumbersSequence : ISequence
18+
{
19+
/// <summary>
20+
/// Gets sequence of Fermat numbers.
21+
/// </summary>
22+
public IEnumerable<BigInteger> Sequence
23+
{
24+
get
25+
{
26+
var n = new BigInteger(2);
27+
28+
while (true)
29+
{
30+
yield return n + 1;
31+
n *= n;
32+
}
33+
}
34+
}
35+
}
36+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ This repository contains algorithms and data structures implemented in C# for ed
104104
* [A000079 Powers of 2](./Algorithms/Sequences/PowersOf2Sequence.cs)
105105
* [A000108 Catalan](./Algorithms/Sequences/CatalanSequence.cs)
106106
* [A000142 Factorial](./Algorithms/Sequences/FactorialSequence.cs)
107+
* [A000215 Fermat Numbers](./Algorithms/Sequences/FermatNumbersSequence.cs)
107108
* [A000290 Squares](./Algorithms/Sequences/SquaresSequence.cs)
108109
* [A000578 Cubes](./Algorithms/Sequences/CubesSequence.cs)
109110
* [A000720 PrimePi](./Algorithms/Sequences/PrimePiSequence.cs)

0 commit comments

Comments
 (0)