'LCM calculation
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){}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
