eigenmath/lcm.cpp

76 lines
717 B
C++
Raw Permalink Normal View History

2006-01-04 22:26:50 +01:00
// Find the least common multiple of two expressions.
2004-03-03 21:24:06 +01:00
2006-01-04 22:26:50 +01:00
#include "stdafx.h"
2004-03-03 21:24:06 +01:00
#include "defs.h"
2006-04-25 20:31:30 +02:00
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);
}
}
2004-03-03 21:24:06 +01:00
void
lcm(void)
{
int x;
x = expanding;
save();
2006-01-04 22:26:50 +01:00
yylcm();
2004-03-03 21:24:06 +01:00
restore();
expanding = x;
}
2006-01-04 22:26:50 +01:00
void
yylcm(void)
2004-03-03 21:24:06 +01:00
{
expanding = 1;
p2 = pop();
p1 = pop();
push(p1);
push(p2);
gcd();
push(p1);
divide();
push(p2);
divide();
inverse();
}
2007-05-08 16:57:30 +02:00
#if SELFTEST
2004-03-03 21:24:06 +01:00
static char *s[] = {
"lcm(4,6)",
"12",
"lcm(4*x,6*x*y)",
"12*x*y",
2006-04-25 20:31:30 +02:00
// multiple arguments
"lcm(2,3,4)",
"12",
2004-03-03 21:24:06 +01:00
};
void
test_lcm(void)
{
test(__FILE__, s, sizeof (s) / sizeof (char *));
}
2007-05-08 16:57:30 +02:00
#endif