lcm.cpp 717 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Find the least common multiple of two expressions.
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. void
  5. eval_lcm(void)
  6. {
  7. p1 = cdr(p1);
  8. push(car(p1));
  9. eval();
  10. p1 = cdr(p1);
  11. while (iscons(p1)) {
  12. push(car(p1));
  13. eval();
  14. lcm();
  15. p1 = cdr(p1);
  16. }
  17. }
  18. void
  19. lcm(void)
  20. {
  21. int x;
  22. x = expanding;
  23. save();
  24. yylcm();
  25. restore();
  26. expanding = x;
  27. }
  28. void
  29. yylcm(void)
  30. {
  31. expanding = 1;
  32. p2 = pop();
  33. p1 = pop();
  34. push(p1);
  35. push(p2);
  36. gcd();
  37. push(p1);
  38. divide();
  39. push(p2);
  40. divide();
  41. inverse();
  42. }
  43. #if SELFTEST
  44. static char *s[] = {
  45. "lcm(4,6)",
  46. "12",
  47. "lcm(4*x,6*x*y)",
  48. "12*x*y",
  49. // multiple arguments
  50. "lcm(2,3,4)",
  51. "12",
  52. };
  53. void
  54. test_lcm(void)
  55. {
  56. test(__FILE__, s, sizeof (s) / sizeof (char *));
  57. }
  58. #endif