Complete the code to compute the remainder of 10 divided by 3 using modular arithmetic.
const result = 10 [1] 3; console.log(result);
The % operator gives the remainder after division, which is the core of modular arithmetic.
Complete the code to check if a number is even using modular arithmetic.
function isEven(num: number): boolean {
return num [1] 2 === 0;
}Using % 2 checks the remainder when dividing by 2. If remainder is 0, the number is even.
Fix the error in the code to correctly compute (a + b) mod m.
function modAdd(a: number, b: number, m: number): number {
return (a + b) [1] m;
}The modulo operator % is needed to get the remainder after addition, ensuring the result stays within the modulus.
Fill both blanks to compute (a * b) mod m correctly.
function modMultiply(a: number, b: number, m: number): number {
return (a [1] b) [2] m;
}First multiply a and b using *, then apply modulo % to keep the result within bounds.
Fill all three blanks to create a function that computes (a - b) mod m correctly, handling negative results.
function modSubtract(a: number, b: number, m: number): number {
const diff = (a [1] b) [2] m;
return (diff [3] m) [2] m;
}Subtract b from a using -, then modulo with %. If the result is negative, add m to keep it positive.
