Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
22
Task/Factorial/C-sharp/factorial-1.cs
Normal file
22
Task/Factorial/C-sharp/factorial-1.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
var accumulator = 1;
|
||||
for (var factor = 1; factor <= number; factor++)
|
||||
{
|
||||
accumulator *= factor;
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
17
Task/Factorial/C-sharp/factorial-2.cs
Normal file
17
Task/Factorial/C-sharp/factorial-2.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
return number == 0 ? 1 : number * Factorial(number - 1);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
27
Task/Factorial/C-sharp/factorial-3.cs
Normal file
27
Task/Factorial/C-sharp/factorial-3.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
return Factorial(number, 1);
|
||||
}
|
||||
|
||||
static int Factorial(int number, int accumulator)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
if(accumulator < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(accumulator), accumulator, "Must be a positive number.");
|
||||
|
||||
return number == 0 ? accumulator : Factorial(number - 1, number * accumulator);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
15
Task/Factorial/C-sharp/factorial-4.cs
Normal file
15
Task/Factorial/C-sharp/factorial-4.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
return Enumerable.Range(1, number).Aggregate((accumulator, factor) => accumulator * factor);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
31
Task/Factorial/C-sharp/factorial-5.cs
Normal file
31
Task/Factorial/C-sharp/factorial-5.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
using System.Linq;
|
||||
class Program
|
||||
{
|
||||
static BigInteger factorial(int n) // iterative
|
||||
{
|
||||
BigInteger acc = 1; for (int i = 1; i <= n; i++) acc *= i; return acc;
|
||||
}
|
||||
|
||||
static public BigInteger Factorial(int number) // functional
|
||||
{
|
||||
return Enumerable.Range(1, number).Aggregate(new BigInteger(1), (acc, num) => acc * num);
|
||||
}
|
||||
|
||||
static public BI FactorialQ(int number) // functional quick, uses prodtree method
|
||||
{
|
||||
var s = Enumerable.Range(1, number).Select(num => new BI(num)).ToArray();
|
||||
int top = s.Length, nt, i, j;
|
||||
while (top > 1) {
|
||||
for (i = 0, j = top, nt = top >> 1; i < nt; i++) s[i] *= s[--j];
|
||||
top = nt + ((top & 1) == 1 ? 1 : 0);
|
||||
}
|
||||
return s[0];
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(Factorial(250));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue