-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactorial.cpp
192 lines (158 loc) · 2.65 KB
/
factorial.cpp
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
#include "stdafx.h"
#include "defs.h"
extern void bignum_factorial(int);
void
factorial(void)
{
int n;
save();
p1 = pop();
push(p1);
n = pop_integer();
if (n < 0 || n == (int) 0x80000000) {
push_symbol(FACTORIAL);
push(p1);
list(2);
restore();
return;
}
bignum_factorial(n);
restore();
}
void sfac_product(void);
void sfac_product_f(U **, int, int);
// simplification rules for factorials (m < n)
//
// (e + 1) * factorial(e) -> factorial(e + 1)
//
// factorial(e) / e -> factorial(e - 1)
//
// e / factorial(e) -> 1 / factorial(e - 1)
//
// factorial(e + n)
// ---------------- -> (e + m + 1)(e + m + 2)...(e + n)
// factorial(e + m)
//
// factorial(e + m) 1
// ---------------- -> --------------------------------
// factorial(e + n) (e + m + 1)(e + m + 2)...(e + n)
void
simplifyfactorials(void)
{
int x;
save();
x = expanding;
expanding = 0;
p1 = pop();
if (car(p1) == symbol(ADD)) {
push(zero);
p1 = cdr(p1);
while (iscons(p1)) {
push(car(p1));
simplifyfactorials();
add();
p1 = cdr(p1);
}
expanding = x;
restore();
return;
}
if (car(p1) == symbol(MULTIPLY)) {
sfac_product();
expanding = x;
restore();
return;
}
push(p1);
expanding = x;
restore();
}
void
sfac_product(void)
{
int i, j, n;
U **s;
s = stack + tos;
p1 = cdr(p1);
n = 0;
while (iscons(p1)) {
push(car(p1));
p1 = cdr(p1);
n++;
}
for (i = 0; i < n - 1; i++) {
if (s[i] == symbol(NIL))
continue;
for (j = i + 1; j < n; j++) {
if (s[j] == symbol(NIL))
continue;
sfac_product_f(s, i, j);
}
}
push(one);
for (i = 0; i < n; i++) {
if (s[i] == symbol(NIL))
continue;
push(s[i]);
multiply();
}
p1 = pop();
tos -= n;
push(p1);
}
void
sfac_product_f(U **s, int a, int b)
{
int i, n;
p1 = s[a];
p2 = s[b];
if (ispower(p1)) {
p3 = caddr(p1);
p1 = cadr(p1);
} else
p3 = one;
if (ispower(p2)) {
p4 = caddr(p2);
p2 = cadr(p2);
} else
p4 = one;
if (isfactorial(p1) && isfactorial(p2)) {
// Determine if the powers cancel.
push(p3);
push(p4);
add();
yyexpand();
n = pop_integer();
if (n != 0)
return;
// Find the difference between the two factorial args.
// For example, the difference between (a + 2)! and a! is 2.
push(cadr(p1));
push(cadr(p2));
subtract();
yyexpand(); // to simplify
n = pop_integer();
if (n == 0 || n == (int) 0x80000000)
return;
if (n < 0) {
n = -n;
p5 = p1;
p1 = p2;
p2 = p5;
p5 = p3;
p3 = p4;
p4 = p5;
}
push(one);
for (i = 1; i <= n; i++) {
push(cadr(p2));
push_integer(i);
add();
push(p3);
power();
multiply();
}
s[a] = pop();
s[b] = symbol(NIL);
}
}