Skip to content

Commit e5231ae

Browse files
committed
I need more comparisons!
1 parent b0ed763 commit e5231ae

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

csharp/Platform.Numbers.Benchmarks/MathBenchmarks.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Platform.Numbers.Benchmarks
66
[MemoryDiagnoser]
77
public class MathBenchmarks
88
{
9-
private const int FACTORIALTESTNUMBER = 10000;
9+
private const int FACTORIALTESTNUMBER = 19;
1010

1111
[Benchmark]
1212
public long FactorialUsingRecursion()
@@ -21,9 +21,15 @@ public long FactorialUsingFactTree()
2121
}
2222

2323
[Benchmark]
24-
public long FactorialUsingRecursionWihoutStaticField()
24+
public long FactorialRecursionCountingArraylength()
2525
{
26-
return Math.FactorialStatic(FACTORIALTESTNUMBER);
26+
return Math.FactorialRecursionCountingArraylength(FACTORIALTESTNUMBER);
27+
}
28+
29+
[Benchmark]
30+
public long FactorialWhileWithArray()
31+
{
32+
return Math.FactorialWhileWithArray(FACTORIALTESTNUMBER);
2733
}
2834
}
2935
}

csharp/Platform.Numbers/Math.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@ public static class Math
4343
/// <returns><para>Result of factorial calculation.</para><para>Результат подсчета факториала</para></returns>
4444
public static long Factorial(long n)
4545
{
46-
long[] _facts =
47-
{
48-
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
49-
479001600, 6227020800, 87178291200, 1307674368000, 20922789888000,
50-
355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000,
51-
};
52-
5346
if (n <= 1)
5447
{
5548
return 1;
@@ -58,7 +51,7 @@ public static long Factorial(long n)
5851

5952
if (n < 21)
6053
{
61-
return _facts[n];
54+
return _factorials[n];
6255
}
6356

6457
return n * Factorial(n - 1);
@@ -70,7 +63,7 @@ public static long Factorial(long n)
7063
/// </summary>
7164
/// <param name="n"><para>Factorial generation value.</para><para>Значение генерации факториала.</para></param>
7265
/// <returns><para>Result of factorial calculation.</para><para>Результат подсчета факториала</para></returns>
73-
public static long FactorialStatic(long n)
66+
public static long FactorialRecursionCountingArraylength(long n)
7467
{
7568
if (n <= 1)
7669
{
@@ -123,6 +116,21 @@ public static long FactTree(int n)
123116
return n * Factorial(n - 1);
124117
}
125118

119+
public static long FactorialWhileWithArray(long n)
120+
{
121+
long[] _facts =
122+
{
123+
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
124+
479001600, 6227020800, 87178291200, 1307674368000, 20922789888000,
125+
355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000,
126+
};
127+
if (n < _facts.Length) return _facts[n];
128+
129+
long r = n;
130+
while (n > 1) r *= --n;
131+
return r;
132+
}
133+
126134
/// <summary>
127135
/// <para>Generating the Catalan Number of the value "n".</para>
128136
/// <para>Генерация числа Каталана из значения переменной "n".</para>

0 commit comments

Comments
 (0)