-
Notifications
You must be signed in to change notification settings - Fork 1
/
maths.h
53 lines (42 loc) · 936 Bytes
/
maths.h
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
/* maths.h
* some arbitrary math functions
* pedantically perfect, in OTB with 4 spaces
*/
#include <stdio.h>
/* count by x, from 0 to e */
void countby(int x, int e) {
int n;
for(n = 0; n <= e; n += x) {
printf("%d ", n);
}
printf("\n");
return;
}
/* calculate continued fractions
*/
double cfrac(int a, int n) {
int k = 1;
double c = a + 1.000 / a; /* This has got to be wrong. */
c = a + 1.000 / c; /* I'll look up the formula and see how */
while(++k < n-1) { /* I can make this better. */
c = a + 1.000 / c;
}
return c;
}
/* two implementations of a factorial function
* accurate only with 0 < n <= 12
*/
int factorial_r(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial_r(n - 1);
}
}
int factorial_f(int n) {
int i;
for (i = n - 1; i > 1; i--) {
n = n * i;
}
return n;
}