Complete the code to calculate the greatest common divisor (GCD) of two numbers using Euclid's algorithm.
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a [1] b;
a = temp;
}
return a;
}The modulo operator (%) gives the remainder, which is used in Euclid's algorithm to find the GCD.
Complete the code to check if a number is prime by testing divisibility up to its square root.
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= [1]; i++) {
if (n % i == 0) return false;
}
return true;
}Checking divisibility up to the square root of n is enough to determine primality efficiently.
Fix the error in the modular exponentiation function to correctly compute (base^exp) % mod.
long long modExp(long long base, long long exp, long long mod) {
long long result = 1;
base = base % mod;
while (exp > 0) {
if (exp [1] 1) {
result = (result * base) % mod;
}
base = (base * base) % mod;
exp = exp / 2;
}
return result;
}The bitwise AND (&) with 1 checks if the exponent is odd, which is essential in modular exponentiation.
Fill both blanks to create a dictionary comprehension that maps numbers to their squares only if the number is even.
{num: num[1]2 for num in range(1, 11) if num [2] 2 == 0}Use '**' for exponentiation and '%' to check if a number is even (remainder zero).
Fill all three blanks to create a dictionary comprehension that maps uppercase letters to their ASCII codes only if the code is greater than 65.
{ [1]: [2] for [3] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' if ord([3]) > 65 }Use 'letter' as the key, 'ord(letter)' as the value, and 'letter' as the loop variable to map letters to ASCII codes.
