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];
a = temp;
}
return a;
}The GCD algorithm uses the remainder of a % b. Here, a % a is incorrect, so the blank must be b.
Complete the code to check if a number is prime by testing divisors 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;
}n - 1 is slower.To check primality efficiently, test divisors only up to the square root of n.
Fix the error in the code to compute the modular exponentiation (power modulo) correctly.
long ModPow(long baseVal, long exponent, long mod) {
long result = 1;
baseVal = baseVal % mod;
while (exponent > 0) {
if (exponent [1] 1 == 1)
result = (result * baseVal) % mod;
baseVal = (baseVal * baseVal) % mod;
exponent = exponent / 2;
}
return result;
}The code checks if the least significant bit of exponent is 1 using bitwise AND &.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
var result = words.ToDictionary(word => word, word => word.[1]).Where(kv => kv.Value [2] 3).ToDictionary(kv => kv.Key, kv => kv.Value);
Count instead of Length for strings.< instead of > in the filter.Use Length property to get word length and filter with > 3.
Fill all three blanks to create a dictionary that maps each word in uppercase to its length, but only include words with length greater than 4.
var filtered = words.Where(word => word.[1] [2] 4); var dict = filtered.ToDictionary(word => word.[3], word => word.Length);
Count instead of Length for strings.< instead of > in the filter.Filter words with Length > 4 and map keys to uppercase using ToUpper().
