nikki@uicsl.csl.uiuc.edu (07/03/90)
Hello everyone, I am looking for a program which is capable of computing LCM (Least Common Multiplier) of many numbers (Max. of ~20). Please send me e-mail if you have such a source code or if you know where I might be able to get it. Thanks in advance, Naghmeh (Nikki) Mirghafori ******************************************************************************* -- - *"I shall pass this way but once, any good; therefore, that I can | \ | | * do or any kindness that I can show to any human being, let me | |\ \ | | * do it now. Let me no defer nor neglect it, for I shall not pass | | \ \ | | * this way again." | | \ \| | * -- Dale Carnegie |_| \___| nikki@uicsl.csl.uiuc.edu****************************************** *******************************************************************************
tif@doorstop.austin.ibm.com (Paul Chamberlain) (07/07/90)
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). E-mail might be tricky -- besides I'm kind of proud of this. I wrote a program (220 lines) that give GCD and LCM. Most of it is a routine that finds the next prime :-). Currently it will take as many numbers as you like on the command line. It is limited to long integer math. For example lcm 20 36 48 72 34 8 38 24 22 112 46 gives LCM = 411863760 GCD = 2 Mail to the path in my signature if you're interested. Paul Chamberlain | I do NOT represent IBM tif@doorstop, sc30661@ausvm6 512/838-7008 | ...!cs.utexas.edu!ibmaus!auschs!doorstop.austin.ibm.com!tif
michaelg@Neon.Stanford.EDU (Michael Greenwald) (07/12/90)
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<x2)?gcd(x,x2):gcd(x2,x))) * x); } else return(x); } If you want lcm to take its arguments from the command line and print result there, then you can add void main(argc, argv) int argc; char* argv[]; { int numbers[MAX], i; for (i=1; i<argc; i++) sscanf(argv[i],"%ld",&numbers[i-1]); printf ("%ld\n", lcm(numbers[argc-2], argc-2, numbers)); }
peter@ficc.ferranti.com (Peter da Silva) (07/12/90)
In article <1990Jul12.004600.7378@Neon.Stanford.EDU> michaelg@Neon.Stanford.EDU (Michael Greenwald) writes: > x2 = lcm (numbers[--n], n, numbers); Not a good idea. N might be decremented before or after the second use, even in pre-ANSI compilers. Try: n--; x2 = lcm(numbers[n], n, numbers); -- Peter da Silva. `-_-' +1 713 274 5180. <peter@ficc.ferranti.com>