Complete the code to compute the remainder of 10 divided by 3.
int result = 10 [1] 3; printf("%d\n", result);
The % operator gives the remainder of division in C.
Complete the code to compute (7 + 5) modulo 6.
int sum = (7 + 5) [1] 6; printf("%d\n", sum);
The modulo operator % gives the remainder after division, so (7+5)%6 = 12%6 = 0.
Fix the error in the code to correctly compute (a * b) modulo m.
int mod_mul(int a, int b, int m) {
return (a [1] b) % m;
}To multiply a and b before modulo, use the * operator.
Fill both blanks to compute (x - y) modulo n correctly, handling negative results.
int mod_sub(int x, int y, int n) {
int diff = (x [1] y) % n;
return diff [2] 0 ? diff + n : diff;
}Subtract y from x, then if result is negative (less than 0), add n to keep it positive modulo.
Fill all three blanks to create a function that computes (a^b) modulo m using fast exponentiation.
int mod_pow(int a, int b, int m) {
int result = 1;
a = a [1] m;
while (b > 0) {
if (b [2] 1) result = (result * a) % m;
a = (a * a) % m;
b = b [3] 1;
}
return result;
}Use modulo to keep a in range, bitwise & 1 to check odd b, and right shift >> 1 to divide b by 2.
