Skip to content

Commit 0891a9c

Browse files
authored
Add A019434 Fermat Primes sequence (TheAlgorithms#299)
1 parent b5b0044 commit 0891a9c

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
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 FermatPrimesSequenceTests
10+
{
11+
[Test]
12+
public void All5ElementsCorrect()
13+
{
14+
var sequence = new FermatPrimesSequence().Sequence.Take(5);
15+
sequence.SequenceEqual(new BigInteger[] { 3, 5, 17, 257, 65537 })
16+
.Should().BeTrue();
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Numerics;
4+
5+
namespace Algorithms.Sequences
6+
{
7+
/// <summary>
8+
/// <para>
9+
/// Sequence of Fermat primes: primes of the form 2^(2^k) + 1, for some k >= 0.
10+
/// </para>
11+
/// <para>
12+
/// Wikipedia: https://wikipedia.org/wiki/Fermat_number.
13+
/// </para>
14+
/// <para>
15+
/// OEIS: https://oeis.org/A019434.
16+
/// </para>
17+
/// </summary>
18+
public class FermatPrimesSequence : ISequence
19+
{
20+
/// <summary>
21+
/// Gets sequence of Fermat primes.
22+
/// </summary>
23+
public IEnumerable<BigInteger> Sequence
24+
{
25+
get
26+
{
27+
var fermatNumbers = new FermatNumbersSequence().Sequence.Take(5);
28+
29+
foreach (var n in fermatNumbers)
30+
{
31+
yield return n;
32+
}
33+
}
34+
}
35+
}
36+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ This repository contains algorithms and data structures implemented in C# for ed
118118
* [A007318 Binomial](./Algorithms/Sequences/BinomialSequence.cs)
119119
* [A010051 Binary Prime Constant](./Algorithms/Sequences/BinaryPrimeConstantSequence.cs)
120120
* [A011557 Powers of 10](./Algorithms/Sequences/PowersOf10Sequence.cs)
121+
* [A019434 Fermat Primes](./Algorithms/Sequences/FermatPrimesSequence.cs)
121122
* [A181391 Van Eck's](./Algorithms/Sequences/VanEcksSequence.cs)
122123
* [String](./Algorithms/Strings)
123124
* [Longest Consecutive Character](./Algorithms/Strings/GeneralStringAlgorithms.cs)

0 commit comments

Comments
 (0)