I wrote code for the problem given in spoj to calculate LCM. I calculated the gcd of 2 numbers and I divided the multiplication of 2 numbers with gcd which gives the lcm of 2 numbers, but it is showing wrong answer.
The problem is at http://www.spoj.com/problems/WPC5I/
import java.math.BigInteger;
import java.util.Scanner;
class Lcm1 {
public static void main(String args[]) throws Throwable {
try {
Scanner s = new Scanner(System.in);
int siz = s.nextInt();
for(int i = 0; i< siz; i++) {
BigInteger a = s.nextBigInteger(), b = s.nextBigInteger();
System.out.println((a.multiply(b)).divide(a.gcd(b)));
}
}
catch(Exception e){}
}
}
Your logic is partially wrong.Basically you are just calculating the lcm of given numbers but user asks to find least k satisfying given condition.but yours is not least k. Try for case n=340 and m=230 actual ans is 1564 but your code gives 7820.
Note: lcm will not always be the least k satisfying given conditions in the problem.
Hint:prime factorize m,n and try to get the ans...try few more cases and you will get it.