Question

In: Computer Science

Your task is to determine the least common multiple of two integers. You will need to...

Your task is to determine the least common multiple of two integers.

You will need to use the Euclidean algorithm for GCD to prevent going above the time limit.

input specification

The first line will contain a single integer NN (1<N<100001

answer must be in c++ language

The following NN lines will each contain a 2 integers aa and bb (1<a,b<1000000001

Solutions

Expert Solution

CODE:

#include <iostream>
using namespace std;

// to find the GCD of two numbers
int gcd(int a, int b){
    // if a divides b perfectly
    if(a%b==0){
        return b;
    }
    // if a can't divide b perfectly
    return gcd(b%a,a);
}

int main()
{
    // input of number of lines
    int n;
    cin>>n;
    
    // iterating for each line
    while(n>0){
        n--;
        
        // input of 2 numbers
        int a,b;
        cin>>a>>b;
        
        // stores the gcd of a and b in r
        int r = gcd(a,b);
        
        // stores the lcm of a and b in lcm
        int lcm = a*b/r;
            
        cout<<lcm<<endl;
    }

    return 0;
}

INPUT:

3                                                         

4 10                                                                                                                                       

5 12                                                                                                                                       

7 10    

OUTPUT:                                                               

20

60

70  


Related Solutions

The least common multiple of nonzero integers a and b is the smallest positive integer m...
The least common multiple of nonzero integers a and b is the smallest positive integer m such that a|m and b|m. It is denoted [a, b], or sometimes [a, b] for short. Prove the following: 1) If a|k and b|k, then [a, b]|k. 2) If gcd(a, b) = 1, then [a, b] =ab 3) If c >0,then [ca, cb] =c·[a, b]. 4) If a >0 and b >0,then [a, b] =ab / gcd(a, b).
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
Write a C++ program to find least common multiple (LCM) of two, three and four integer...
Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the console, calculate LCM using Prime factorization method. Your program should...
Write a C++ program to find least common multiple (LCM) of two, three and four integer...
Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the console, calculate LCM using Prime factorization method. Your program should...
Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number.
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
IN PYTHON : Write one program that will compute both the Least Common Multiple, and the...
IN PYTHON : Write one program that will compute both the Least Common Multiple, and the Greatest Common Factor of two numbers inputted by a user. The program will display all factors and multiples of the two numbers.   When displaying the multiples of the numbers, only display up to the first input times the second input. \ Use functions to computer the answers.   Loop the program so that the user can enter two new numbers after each try.
1. When designing an experiment to test your hypothesis, you need at least two different conditions....
1. When designing an experiment to test your hypothesis, you need at least two different conditions. In the “experimental” condition one or more of the variables are altered to test the hypothesis. The other condition is called the “control” condition. In your own words, what is the purpose of the control condition during the experiment. (3 points) 2. Ionic and covalent bonds between atoms are necessary to build molecules and elements. What is a key difference between ionic bonds and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT