-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cs
125 lines (114 loc) · 3.5 KB
/
Functions.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
class Functions {
public static double Average(List<double> args) {
if (args.Count == 0) {
throw new InvalidOperationException("Average requires 1 or more arguments.");
}
double average = args[0];
for (int i = 1; i < args.Count; i++) {
average += args[i];
}
//double average = ;
//foreach (double d in args) {
// average += d;
//}
return average / args.Count;
}
public static void CheckArgs(List<double> args, int excpectedArgCount, string name) {
if (args.Count != excpectedArgCount) {
throw new Exception(name + " requires " + excpectedArgCount + " arguments.");
}
return;
}
public static double Factorial(double number) {
int i = (int)number;
if (number != i || i < 0) {
throw new Exception(string.Format("The factorial is not defined for {0}", number));
}
return Factorial(i);
}
private static double Factorial(int number) {
double dnumber = 1;
while (number > 1) {
dnumber *= number;
number--;
if (double.IsInfinity(dnumber)) {
break;
}
}
return dnumber;
}
public static double GCF(double num1, double num2, bool checkWhole) {
if (checkWhole) {
long l1 = (long)num1;
long l2 = (long)num2;
if (l1 != num1 || l2 != num2) {
throw new Exception("Arguments of GCF must be whole numbers");
}
}
while (num2 != 0) {
double temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
public static bool IsPrime(double n) {
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
if (n < 2) {
return false;
}
//1 is added because of rounding
//Prime numbers are of form 6*k+1 and 6*k-1
for (long k = 1; k <= ((long)Math.Sqrt(n)) / 6 + 1; k++) {
if (n % (6 * k + 1) == 0 || n % (6 * k - 1) == 0) {
return false;
}
}
return true;
}
public static double LCM(double num1, double num2) {
long l1 = (long)num1;
long l2 = (long)num2;
if (l1 != num1 || l2 != num2) {
throw new Exception("Arguments of LCM must be whole numbers");
}
return (num1 * num2) / GCF(num1, num2, false);
}
public static double Max(List<double> args) {
if (args.Count == 0) {
throw new InvalidOperationException("Max requires 1 or more arguments.");
}
double max = args[0];
for (int i = 1; i < args.Count; i++) {
if (args[i] > max) {
max = args[i];
}
}
return max;
}
public static double Min(List<double> args) {
if (args.Count == 0) {
throw new InvalidOperationException("Min requires 1 or more arguments.");
}
double min = args[0];
for (int i = 1; i < args.Count; i++) {
if (args[i] < min) {
min = args[i];
}
}
return min;
}
public static double ToRadian(double angle) {
return Math.PI * angle / 180;
}
public static double ToDegree(double angle) {
return angle * (180.0 / Math.PI);
}
}