Monday, February 7, 2011

AOJ 0005 GCD and LCM

GCD and LCM

Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b (0 < a, b ≤ 2,000,000,000). You can supporse that LCM(a, b) ≤ 2,000,000,000.

Input

Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.

Output

For each data set, print GCD and LCM separated by a single space in a line.

Sample Input

8 6
50000000 30000000
Output for the Sample Input

2 24
10000000 150000000

#include <iostream>

using namespace::std;

int main(){

  int a,b;
  int gcd;
  int temp, temp2;

  while(cin >> a >> b){

    if (a < b){
      temp = a;
      a = b;
      b = temp;
    }

    gcd = b;
    temp = a;
  
    while(temp%gcd){
      temp2 = gcd;
      gcd = temp%gcd;
      temp = temp2;
    }

    cout << gcd << " " << a/gcd*b << endl;
  }

}

No comments:

Post a Comment