This was inituially a troubling proposition because I’d mixed up “least common multiple” with greatest common factor. Further, the question posed to solve the LCM of three prime integers, namely; three, five and seven. I answered the question as “duh…one.” and moved on. Thx to fellow wikireviewer Eric Barnes who noted the error. I looked up the euclidean definition of what LCM really means…then coded two functions and tested them in a main class. The code is adequately introduced, commented and documented to explain the algorithm’s process, which is essetailly to calculate the LCM of 5 & 7, then save that 1st lcm and plug it into a separate function that will calulate the final LCM using 3 and “lcm of 5 & 7” as parameters.

/**we define the lcm of three ints as follows:
* LCM(a,b,c)=LCM(LCM(a,b),c)=LCM(a,LCM(b,c))
* the lcm of 3, 5 & 7 is, in fact 105
* last stable build 1/24/16 @ssstudios, los angeles, ca
* IP! &c Tapper7.com, ssstudios and lonely faction productions*/
package gcd;
public class Gcd{
public static int gcd(int a,int b){
int t;
while(b!=0){
t = b;
b = a%b;
a = t;
}
return a;
}//end fxn gcd
public static int lcm(int x, int y){
return(x*y/gcd(x,y));
}//end fxn lcm
public static void main(String[] args){
int lcm1 = 0; //will store the lcm of parameter 2 & 3
int lcm2 = 0; //will store the result of lcm(p1, lcm(p2,p3))
int p1 = 3;
int p2 = 5;
int p3 = 7;
lcm1 = lcm(p2,p3);
lcm2 = lcm(lcm1, p1);
System.out.println("The least common multiple of " +p1+ ", " + p2 + ", "
+p3+ " is " +lcm2);
}//end mn
}//end gcd/lcm class tester

here is the result:

run:
The least common multiple of 3, 5, 7 is 105
BUILD SUCCESSFUL (total time: 0 seconds)

therefore, Barnes is correct. Thank you for pointing out my foible and giving a chance to review & test this classic algorithm. I just set up an entirely new IDE on an entirely new system so this made for great QC on the new devkit & system too.


Chris Welke

"walk into the club like what up? I got a big cock!" -Macklemore

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.