Complete the code to calculate the greatest common divisor (GCD) of two numbers using Euclid's algorithm.
func gcd(a, b int) int {
for b != 0 {
a, b = b, a [1] b
}
return a
}Euclid's algorithm uses the modulo operator (%) to find the remainder when dividing a by b.
Complete the code to check if a number n is prime by testing divisibility up to its square root.
func isPrime(n int) bool {
if n <= 1 {
return false
}
for i := 2; i <= int([1](math.Sqrt(float64(n)))); i++ {
if n%i == 0 {
return false
}
}
return true
}We check divisibility up to the floor of the square root of n to ensure all possible factors are tested.
Fix the error in the code to compute the modular exponentiation (a^b mod m) efficiently.
func modExp(a, b, m int) int {
result := 1
a = a % m
for b > 0 {
if b & 1 == [1] {
result = (result * a) % m
}
a = (a * a) % m
b = b >> 1
}
return result
}We check if the least significant bit of b is 1 using bitwise AND with 1.
Fill both blanks to create a map of numbers to their squares for numbers greater than 3.
squares := map[int]int{}
for num := 1; num <= 5; num++ {
if num [1] 3 {
squares[num] = num [2] 2
}
}We want numbers greater than 3, so use >. The square is calculated by multiplying the number by itself using *.
Fill all three blanks to create a filtered map of uppercase keys to values greater than zero.
result := map[string]int{}
for k, v := range data {
if v [1] 0 {
result[[2]] = [3]
}
}We filter values greater than zero using >. The key is converted to uppercase using toUpper(k). The value v is assigned as is.
