-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factorila.cs
85 lines (67 loc) · 2.15 KB
/
Factorila.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
namespace factorial
{
class Program
{
static void Main(string[] args)
{
//const int n = 5;
//for (var i = 0; i < args.Length; ++i)
//{
// Console.WriteLine($"{args[i]}");
//}
//foreach (var a in args)
//{
// Console.WriteLine(a);
//}
//Array.ForEach(args, a => Console.WriteLine(a));
////OR
//Array.ForEach(args, Console.WriteLine
if (args.Length != 1)
{
Console.WriteLine("You must pass exactly one argument");
Environment.Exit(-1);
}
var n = int.Parse(args[0]);
Console.WriteLine($"Factorial of {n} is {Factorial(n)}");
Console.WriteLine($"Factorial of {n} is {Factorial_tailRecursive(n)}");
Console.WriteLine($"Factorial of {n} is {Factorial_for(n)}");
Console.WriteLine($"Factorial of {n} is {Factorial_LinqProduct(n)}");
Console.WriteLine($"Fib: {Fib(n)}");
}
static int Factorial(int n)
{
// Base cae, n==1
if (n == 1) return 1;
//Otherwise
checked
{
return n * Factorial(n - 1);
}
}
static int Factorial_tailRecursive(int n, int accumulator = 1)
{
// Base cae, n==1
if (n == 1) return accumulator;
//Otherwise
return Factorial_tailRecursive(n - 1, accumulator * n);
}
static int Factorial_for(int n)
{
var accum = 1;
for (int i = 1; i <= n; ++i)
{
accum *= i;
}
return accum;
}
static int Factorial_LinqProduct(int n) => Enumerable.Range(1, n).Aggregate((x, y) => x * y);
static int Fib(int n, int x = 0, int y = 1)
{
if (n == 0) return x;
return Fib(n - 1, y, x + y);
}
}
}