-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcm.cpp
75 lines (58 loc) · 723 Bytes
/
lcm.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
// Find the least common multiple of two expressions.
#include "stdafx.h"
#include "defs.h"
void
eval_lcm(void)
{
p1 = cdr(p1);
push(car(p1));
eval();
p1 = cdr(p1);
while (iscons(p1)) {
push(car(p1));
eval();
lcm();
p1 = cdr(p1);
}
}
void
lcm(void)
{
int x;
x = expanding;
save();
yylcm();
restore();
expanding = x;
}
void
yylcm(void)
{
expanding = 1;
p2 = pop();
p1 = pop();
push(p1);
push(p2);
gcd();
push(p1);
divide();
push(p2);
divide();
inverse();
}
#if SELFTEST
static const char *s[] = {
"lcm(4,6)",
"12",
"lcm(4*x,6*x*y)",
"12*x*y",
// multiple arguments
"lcm(2,3,4)",
"12",
};
void
test_lcm(void)
{
test(__FILE__, s, sizeof (s) / sizeof (char *));
}
#endif