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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//TODO: Binary search and some sorting functions needed to be added
pub mod numbers {
pub fn pi() -> f32 {
return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
}
}
#[allow(dead_code)]
pub mod list {
//This function is used to sort the list. (Bubble sort)
pub fn bubble_sort(list: &mut [usize]) {
for _ in 0..list.len() {
for j in 0..(&list.len() - 1) {
if list[j] > list[j + 1] {
list.swap(j, j + 1);
}
}
}
}
pub fn reverse_bsort(list: &mut [usize]) {
for _ in 0..list.len() {
for j in 0..(&list.len() - 1) {
if list[j] < list[j + 1] {
list.swap(j, j + 1);
}
}
}
}
//THIS FUNCTION NEEDS SORTED ARRAY
pub fn med(list: &[usize]) -> i32 {
let len = list.len();
if len % 2 == 0 {
let med1 = list[len / 2];
let med2 = list[len / 2 - 1];
let med = (med1 + med2) / 2;
return med as i32;
} else {
let med = list[(len - 1) / 2];
return med as i32;
}
}
//This function works whether the list is sorted or not, if sorted, returns med of the list, else -1. Uses extra resource. (May be a problem for old systems)
pub fn safe_med(list: &[usize]) -> i32 {
if check_sorted(list) {
return med(list);
} else {
return -1;
}
}
//This function returns the average of the list.
pub fn avg(list: &[usize]) -> f32 {
let mut sum = 0;
for i in list {
sum += i;
}
let avg = sum as f32 / list.len() as f32;
return avg;
}
//This function returns the mode of the list.
pub fn mode(list: &[usize]) -> i32 {
let mut mode = 0;
let mut max = 0;
for i in list {
let mut count = 0;
for j in list {
if i == j {
count += 1;
}
}
if count > max {
max = count;
mode = *i;
}
}
return mode as i32;
}
//This function returns the standard deviation of the list.
pub fn std_deviation(list: &[usize]) -> f32 {
let avg = avg(list);
let mut sum: f32 = 0.00;
let len = list.len() as f32 - 1.00;
for i in list {
sum += super::num::abs((*i as f32 - avg).powf(2.00));
}
let std_dev = (sum / len).sqrt();
return std_dev;
}
//This function returns the highest number in the list.
pub fn highest(list: &[usize]) -> i32 {
let mut peak = list[0];
for i in list {
if *i > peak {
peak = *i;
}
}
return peak as i32;
}
//This function returns the lowest number in the list.
pub fn lowest(list: &[usize]) -> i32 {
let mut low = list[0];
for i in list {
if *i < low {
low = *i;
}
}
return low as i32;
}
//This function returns the true if the list is forted, else false.
pub fn check_sorted(list: &[usize]) -> bool {
for i in 0..list.len() - 1 {
if list[i] > list[i + 1] {
return false;
}
}
return true;
}
}
#[allow(dead_code)]
pub mod num {
pub fn abs(num: f32) -> f32 {
if num < 0.0 {
return -num;
} else {
return num;
}
}
pub fn sqrt(num: f32) -> f32 {
let mut number = num;
while abs(num - number * number) > 0.00001 {
number = (number + num / number) / 2.00;
}
return number;
}
//This function returns the factorial of the number.
pub fn factorial(num: i32) -> i128 {
if num == 0 {
return 1;
} else if num > 0 {
return num as i128 * factorial(num - 1);
} else {
return -1;
}
}
//This function checks if number prime or not
pub fn is_prime(number: i32) -> bool {
/*
Using 6k + 1 optimization: all numbers are expresible as 6k + i with i = {-1, 0, 1, 2, 3, 4}.
Test if n is divisible by 2 or 3, with that you remove: 6k, 6k + 2, 6k + 4, 6k + 3.
This means that you only have to test numbers of the form 6k - 1 and 6k + 1 less than sqrt(n)
These numbers are 5, 11, 17, 23, 29, 35... and 7, 13, 19, 25, 31, 37...
*/
if number == 2 || number == 3 {
return true;
}
if number % 2 == 0 || number % 3 == 0 {
return false;
}
for i in (1..).map(|k| 6 * k - 1).take_while(|m| m * m <= number) {
if number % i == 0 || number % (i + 2) == 0 {
return false;
}
}
return true;
}
//This function finds greatest common divisor of two numbers
pub fn gcd(a: i32, b: i32) -> i32 {
if a == 0 {
return b;
} else {
return gcd(b % a, a);
}
}
//This function finds least common multiple of two numbers
pub fn lcm(a: i32, b: i32) -> i32 {
return a * b / gcd(a, b);
}
//This is for finding prime factors
pub fn prime_factor(int32: i32) -> Vec<i32> {
let mut c = 2;
let mut iint32 = int32;
let mut ret: Vec<i32> = Vec::new();
while iint32 > 1 {
if iint32 % c == 0 {
ret.push(c);
iint32 /= c;
} else {
c += 1;
}
}
return ret;
}
}
#[allow(dead_code)]
pub mod trigonometry {
use super::numbers::pi;
//This function converts a number from degrees to radians.
pub fn deg2rad(x: f32) -> f32 {
return x * pi() / 180.00;
}
//This function finds quadrant of angle.
pub fn find_quadrant(angle: f32) -> i32 {
if angle >= 360.00 {
return find_quadrant(angle - 360.00);
} else if angle <= 0.00 {
return find_quadrant(angle + 360.00);
} else {
return ((angle / 90.00) + 1.00) as i32;
}
}
//This function convert the number from radians to degrees.
pub fn rad2deg(x: f32) -> f32 {
return x * 180.00 / pi();
}
//This function finds the sine of the angle.
pub fn sin(ang: f32) -> f32 {
//Algorithm for calculating sine
//1. Use periodicity
//If angle is between 0 and 360, no problem, but if not, reduce x so that it lies in the range 0≤x≤360° by adding or subtracting a suitable multiple of 360° from it (we are assuming that angle x is measured in degrees).
let mut angle = ang;
while angle < 0.0 {
angle += 360.0;
}
while angle > 360.0 {
angle -= 360.0;
}
//2. Using symetry
//If angle is quadrant 1, go to step 3.
let quadrant = find_quadrant(angle);
if quadrant == 2 {
angle = 180.0 - angle;
} else if quadrant == 3 {
angle = 180.0 - angle;
} else if quadrant == 4 {
angle = angle - 360.0;
}
//3. Using the cofunction
if angle >= 45.0 {
return cos(90.0 - angle);
} else if angle < 45.0 {
//4. Using the sine polynomial
angle = deg2rad(angle);
return angle - (angle.powf(3.0) / 6.0) + (angle.powf(5.0) / 120.0);
}
return -1.0;
}
pub fn cos(ang: f32) -> f32 {
//1. Use periodicity
let mut angle = ang;
while angle < 0.0 {
angle += 360.0;
}
while angle > 360.0 {
angle -= 360.0;
}
//2. Use symetry
let quadrant = find_quadrant(angle);
if quadrant == 1 {
//3. Using cofunction
if angle >= 45.0 {
return sin(90.0 - angle);
} else if angle < 45.0 {
angle = deg2rad(angle);
//4. Using cosine polynomial
return 1.0 - (angle.powf(2.0) / 2.0) + (angle.powf(4.0) / 24.0)
- (angle.powf(6.0) / 720.0);
}
} else if quadrant == 2 {
angle = 180.0 - angle;
return -1.0 * cos(angle);
} else if quadrant == 3 {
angle = angle - 180.0;
return -1.0 * cos(angle);
} else if quadrant == 4 {
angle = 360.0 - angle;
return cos(angle);
}
return -1.0;
}
pub fn tan(ang: f32) -> f32 {
let mut angle = ang;
//1. Reduce angle between 180 and 0
while angle > 180.0 {
angle -= 180.0;
}
while angle < 0.0 {
angle += 180.0;
}
//2. Reduce angle between 90 and 0
let quadrant = find_quadrant(angle);
if quadrant == 2 {
return -1.0 * tan(180.0 - angle);
} else if quadrant == 1 {
if angle >= 45.0 {
//3. Cofunction
return 1.0 / tan(90.0 - angle);
} else if angle < 45.0 {
if angle >= 22.5 {
return (2.0 * tan(angle / 2.0)) / (1.0 - tan(angle / 2.0).powf(2.0));
} else if angle < 22.5 {
angle = deg2rad(angle);
return angle
+ (angle.powf(3.0) / 3.0)
+ (2.0 * angle.powf(5.0) / 15.0)
+ (17.0 * angle.powf(7.0) / 315.0);
}
}
}
return -1.0;
}
pub fn cotan(ang: f32) -> f32 {
return 1.0 / tan(ang);
}
pub fn sec(ang: f32) -> f32 {
return 1.0 / cos(ang);
}
pub fn cosec(ang: f32) -> f32 {
return 1.0 / sin(ang);
}
pub fn arctan(x: f32) -> f32 {
let y = x;
if y < 0.0 {
return -1.0 * arctan(-1.0 * y);
} else
/* if positive */
{
if y < 1.0 {
if y <= 0.267949 {
return y - (y.powf(3.0) / 3.0) + (y.powf(5.0) / 5.0);
} else {
return rad2deg(
(super::numbers::pi() / 6.0)
+ arctan((super::num::sqrt(3.0) * y) - 1.0)
/ arctan(super::num::sqrt(3.0) + y),
);
}
} else {
return (super::numbers::pi() / 2.0) - arctan(1.0 / y);
}
}
}
pub fn arcsin(x: f32) -> f32 {
return arctan(x / super::num::sqrt(1.0 - x.powf(2.0)));
}
pub fn arccos(x: f32) -> f32 {
return arctan(super::num::sqrt(1.0 - x.powf(2.0)) / x);
}
}