Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!ucsd!ucbvax!agate!shelby!neon!michaelg From: michaelg@Neon.Stanford.EDU (Michael Greenwald) Newsgroups: comp.unix.questions Subject: Re: LCM: Least Common Multiplier Message-ID: <1990Jul12.004600.7378@Neon.Stanford.EDU> Date: 12 Jul 90 00:46:00 GMT References: <17000004@uicsl.csl.uiuc.edu> <2683@awdprime.UUCP> Organization: Computer Science Department, Stanford University Lines: 52 tif@doorstop.austin.ibm.com (Paul Chamberlain) writes: >In article <17000004@uicsl.csl.uiuc.edu> nikki@uicsl.csl.uiuc.edu writes: >>I am looking for a program which is capable of computing LCM (Least Common >>Multiplier) of many numbers (Max. of ~20). (By the way, lcm is a standard part of Common Lisp which might be available on your machine. (There are a couple of implementations that run on most Unix platforms). I assume, therefore, that you want it in C). You can write it pretty simply as follows. #define MAX 64 /* Not a general purpose gcd - this one expects non-negative integers only, and x < y. Sufficient for lcm, though. */ long gcd(x, y) long x, y; {return((x == 0)?y:gcd(y%x, x));} /* Assumes that X and all elements of NUMBERS are nonnegative. Takes least common multiple of X and the first N integers in the array called NUMBERS */ long lcm(x, n, numbers) long x, n, numbers[]; { if ((n > 0) && (x != 0)) { long x2; x2 = lcm (numbers[--n], n, numbers); return ((x2 / ((x