Skip to content

Commit 691d270

Browse files
authored
Switch to file-scoped namespaces (#432)
1 parent fdfc765 commit 691d270

36 files changed

+931
-970
lines changed

Algorithms/Sequences/BinaryPrimeConstantSequence.cs

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
using System.Collections.Generic;
22
using System.Numerics;
33

4-
namespace Algorithms.Sequences
4+
namespace Algorithms.Sequences;
5+
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of binary prime constant
9+
/// (Characteristic function of primes: 1 if n is prime, else 0).
10+
/// </para>
11+
/// <para>
12+
/// Wikipedia: https://wikipedia.org/wiki/Prime_constant.
13+
/// </para>
14+
/// <para>
15+
/// OEIS: https://oeis.org/A010051.
16+
/// </para>
17+
/// </summary>
18+
public class BinaryPrimeConstantSequence : ISequence
519
{
620
/// <summary>
7-
/// <para>
8-
/// Sequence of binary prime constant
9-
/// (Characteristic function of primes: 1 if n is prime, else 0).
10-
/// </para>
11-
/// <para>
12-
/// Wikipedia: https://wikipedia.org/wiki/Prime_constant.
13-
/// </para>
14-
/// <para>
15-
/// OEIS: https://oeis.org/A010051.
16-
/// </para>
21+
/// Gets sequence of binary prime constant.
1722
/// </summary>
18-
public class BinaryPrimeConstantSequence : ISequence
23+
public IEnumerable<BigInteger> Sequence
1924
{
20-
/// <summary>
21-
/// Gets sequence of binary prime constant.
22-
/// </summary>
23-
public IEnumerable<BigInteger> Sequence
25+
get
2426
{
25-
get
26-
{
27-
ISequence primes = new PrimesSequence();
28-
var n = new BigInteger(0);
27+
ISequence primes = new PrimesSequence();
28+
var n = new BigInteger(0);
2929

30-
foreach (var p in primes.Sequence)
30+
foreach (var p in primes.Sequence)
31+
{
32+
for (n++; n < p; n++)
3133
{
32-
for (n++; n < p; n++)
33-
{
34-
yield return 0;
35-
}
36-
37-
yield return 1;
34+
yield return 0;
3835
}
36+
37+
yield return 1;
3938
}
4039
}
4140
}
+45-46
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,66 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Numerics;
33

4-
namespace Algorithms.Sequences
4+
namespace Algorithms.Sequences;
5+
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of binomial coefficients.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient.
12+
/// </para>
13+
/// <para>
14+
/// OEIS: http://oeis.org/A007318.
15+
/// </para>
16+
/// </summary>
17+
public class BinomialSequence : ISequence
518
{
619
/// <summary>
7-
/// <para>
8-
/// Sequence of binomial coefficients.
9-
/// </para>
10-
/// <para>
11-
/// Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient.
12-
/// </para>
13-
/// <para>
14-
/// OEIS: http://oeis.org/A007318.
15-
/// </para>
20+
/// Gets sequence of binomial coefficients.
1621
/// </summary>
17-
public class BinomialSequence : ISequence
22+
public IEnumerable<BigInteger> Sequence
1823
{
19-
/// <summary>
20-
/// Gets sequence of binomial coefficients.
21-
/// </summary>
22-
public IEnumerable<BigInteger> Sequence
24+
get
2325
{
24-
get
25-
{
26-
var i = 0;
26+
var i = 0;
2727

28-
while (true)
28+
while (true)
29+
{
30+
var row = GenerateRow(i);
31+
foreach (var coefficient in row)
2932
{
30-
var row = GenerateRow(i);
31-
foreach (var coefficient in row)
32-
{
33-
yield return coefficient;
34-
}
35-
36-
i++;
33+
yield return coefficient;
3734
}
35+
36+
i++;
3837
}
3938
}
39+
}
4040

41-
private static BigInteger BinomialCoefficient(long n, long k)
41+
private static BigInteger BinomialCoefficient(long n, long k)
42+
{
43+
if (k == 0 || k == n)
4244
{
43-
if (k == 0 || k == n)
44-
{
45-
return new BigInteger(1);
46-
}
47-
48-
if (n < 0)
49-
{
50-
return new BigInteger(0);
51-
}
52-
53-
return BinomialCoefficient(n - 1, k) + BinomialCoefficient(n - 1, k - 1);
45+
return new BigInteger(1);
5446
}
5547

56-
private static IEnumerable<BigInteger> GenerateRow(long n)
48+
if (n < 0)
5749
{
58-
long k = 0;
50+
return new BigInteger(0);
51+
}
5952

60-
while (k <= n)
61-
{
62-
yield return BinomialCoefficient(n, k);
63-
k++;
64-
}
53+
return BinomialCoefficient(n - 1, k) + BinomialCoefficient(n - 1, k - 1);
54+
}
55+
56+
private static IEnumerable<BigInteger> GenerateRow(long n)
57+
{
58+
long k = 0;
59+
60+
while (k <= n)
61+
{
62+
yield return BinomialCoefficient(n, k);
63+
k++;
6564
}
6665
}
6766
}

Algorithms/Sequences/CatalanSequence.cs

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
using System.Collections.Generic;
22
using System.Numerics;
33

4-
namespace Algorithms.Sequences
4+
namespace Algorithms.Sequences;
5+
6+
/// <summary>
7+
/// <para>
8+
/// Catalan numbers: C[n+1] = (2*(2*n+1)*C[n])/(n+2).
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Catalan_number.
12+
/// </para>
13+
/// <para>
14+
/// OEIS:http://oeis.org/A000108.
15+
/// </para>
16+
/// </summary>
17+
public class CatalanSequence : ISequence
518
{
619
/// <summary>
7-
/// <para>
8-
/// Catalan numbers: C[n+1] = (2*(2*n+1)*C[n])/(n+2).
9-
/// </para>
10-
/// <para>
11-
/// Wikipedia: https://en.wikipedia.org/wiki/Catalan_number.
12-
/// </para>
13-
/// <para>
14-
/// OEIS:http://oeis.org/A000108.
15-
/// </para>
20+
/// Gets sequence of Catalan numbers.
1621
/// </summary>
17-
public class CatalanSequence : ISequence
22+
public IEnumerable<BigInteger> Sequence
1823
{
19-
/// <summary>
20-
/// Gets sequence of Catalan numbers.
21-
/// </summary>
22-
public IEnumerable<BigInteger> Sequence
24+
get
2325
{
24-
get
26+
// initialize the first element (1) and define it's enumerator (0)
27+
var catalan = new BigInteger(1);
28+
var n = 0;
29+
while (true)
2530
{
26-
// initialize the first element (1) and define it's enumerator (0)
27-
var catalan = new BigInteger(1);
28-
var n = 0;
29-
while (true)
30-
{
31-
yield return catalan;
32-
catalan = (2 * (2 * n + 1) * catalan) / (n + 2);
33-
n++;
34-
}
31+
yield return catalan;
32+
catalan = (2 * (2 * n + 1) * catalan) / (n + 2);
33+
n++;
3534
}
3635
}
3736
}

Algorithms/Sequences/CubesSequence.cs

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
using System.Collections.Generic;
22
using System.Numerics;
33

4-
namespace Algorithms.Sequences
4+
namespace Algorithms.Sequences;
5+
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of cube numbers.
9+
/// </para>
10+
/// <para>
11+
/// Wikipedia: https://en.wikipedia.org/wiki/Cube_(algebra).
12+
/// </para>
13+
/// <para>
14+
/// OEIS: https://oeis.org/A000578.
15+
/// </para>
16+
/// </summary>
17+
public class CubesSequence : ISequence
518
{
619
/// <summary>
7-
/// <para>
8-
/// Sequence of cube numbers.
9-
/// </para>
10-
/// <para>
11-
/// Wikipedia: https://en.wikipedia.org/wiki/Cube_(algebra).
12-
/// </para>
13-
/// <para>
14-
/// OEIS: https://oeis.org/A000578.
15-
/// </para>
20+
/// Gets sequence of cube numbers.
1621
/// </summary>
17-
public class CubesSequence : ISequence
22+
public IEnumerable<BigInteger> Sequence
1823
{
19-
/// <summary>
20-
/// Gets sequence of cube numbers.
21-
/// </summary>
22-
public IEnumerable<BigInteger> Sequence
24+
get
2325
{
24-
get
25-
{
26-
var n = BigInteger.Zero;
26+
var n = BigInteger.Zero;
2727

28-
while (true)
29-
{
30-
yield return n * n * n;
31-
n++;
32-
}
28+
while (true)
29+
{
30+
yield return n * n * n;
31+
n++;
3332
}
3433
}
3534
}

Algorithms/Sequences/DivisorsCountSequence.cs

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
using System.Collections.Generic;
22
using System.Numerics;
33

4-
namespace Algorithms.Sequences
4+
namespace Algorithms.Sequences;
5+
6+
/// <summary>
7+
/// <para>
8+
/// Sequence of the number of divisors of n, starting with 1.
9+
/// </para>
10+
/// <para>
11+
/// OEIS: https://oeis.org/A000005.
12+
/// </para>
13+
/// </summary>
14+
public class DivisorsCountSequence : ISequence
515
{
616
/// <summary>
7-
/// <para>
8-
/// Sequence of the number of divisors of n, starting with 1.
9-
/// </para>
10-
/// <para>
11-
/// OEIS: https://oeis.org/A000005.
12-
/// </para>
17+
/// Gets sequence of number of divisors for n, starting at 1.
1318
/// </summary>
14-
public class DivisorsCountSequence : ISequence
19+
public IEnumerable<BigInteger> Sequence
1520
{
16-
/// <summary>
17-
/// Gets sequence of number of divisors for n, starting at 1.
18-
/// </summary>
19-
public IEnumerable<BigInteger> Sequence
21+
get
2022
{
21-
get
23+
yield return BigInteger.One;
24+
for (var n = new BigInteger(2); ; n++)
2225
{
23-
yield return BigInteger.One;
24-
for (var n = new BigInteger(2); ; n++)
26+
var count = 2;
27+
for (var k = 2; k < n; k++)
2528
{
26-
var count = 2;
27-
for (var k = 2; k < n; k++)
29+
BigInteger.DivRem(n, k, out var remainder);
30+
if (remainder == 0)
2831
{
29-
BigInteger.DivRem(n, k, out var remainder);
30-
if (remainder == 0)
31-
{
32-
count++;
33-
}
32+
count++;
3433
}
35-
36-
yield return count;
3734
}
35+
36+
yield return count;
3837
}
3938
}
4039
}

0 commit comments

Comments
 (0)