Complete the code to calculate the GCD of two numbers using the Euclidean algorithm.
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % [1];
a = temp;
}
return a;
}The Euclidean algorithm uses the remainder of a divided by b, so the blank should be 'b' to compute 'a % b'.
Complete the code to calculate the LCM of two numbers using their GCD.
int lcm(int a, int b) {
return (a * b) / [1](a, b);
}The LCM is calculated by dividing the product of the two numbers by their GCD.
Fix the error in the recursive GCD function code.
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd([1], a % b);
}In the recursive call, the first argument should be 'b' and the second 'a % b' to correctly apply the Euclidean algorithm.
Fill both blanks to create a function that returns a map of numbers to their GCD with a fixed number.
std::map<int, int> gcd_map(const std::vector<int>& nums, int fixed) {
std::map<int, int> result;
for (int num : nums) {
result[num] = gcd([1], [2]);
}
return result;
}The gcd function should be called with the current number 'num' and the fixed number 'fixed'.
Fill both blanks to create a function that returns a map of numbers to their LCM with a fixed number.
std::map<int, int> lcm_map(const std::vector<int>& nums, int fixed) {
std::map<int, int> result;
for (int num : nums) {
result[num] = (num * fixed) / [1](num, [2]);
}
return result;
}The LCM is calculated by dividing the product of 'num' and 'fixed' by their GCD. The function to use is 'gcd', and the second argument is 'fixed'.
