Challenge - 5 Problems
Modular Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Modular Addition
What is the output of the following JavaScript code that performs modular addition?
DSA Javascript
const a = 17; const b = 23; const mod = 10; const result = (a + b) % mod; console.log(result);
Attempts:
2 left
💡 Hint
Remember to add first, then apply the modulo operation.
✗ Incorrect
The sum of 17 and 23 is 40. Taking 40 modulo 10 gives the remainder 0, so the correct output is 0.
❓ Predict Output
intermediateModular Multiplication Result
What will be printed after running this JavaScript code that calculates modular multiplication?
DSA Javascript
const x = 7; const y = 8; const mod = 5; const product = (x * y) % mod; console.log(product);
Attempts:
2 left
💡 Hint
Multiply first, then take modulo.
✗ Incorrect
7 times 8 is 56. 56 modulo 5 is 1 because 5 * 11 = 55 and remainder is 1.
❓ Predict Output
advancedModular Exponentiation Output
What is the output of this code that calculates modular exponentiation using repeated multiplication?
DSA Javascript
let base = 3; let exponent = 4; let mod = 7; let result = 1; for(let i = 0; i < exponent; i++) { result = (result * base) % mod; } console.log(result);
Attempts:
2 left
💡 Hint
Calculate 3^4 mod 7 step by step.
✗ Incorrect
3^4 = 81. 81 modulo 7 is 4 because 7 * 11 = 77 and remainder is 4.
🧠 Conceptual
advancedProperties of Modular Arithmetic
Which of the following statements about modular arithmetic is TRUE?
Attempts:
2 left
💡 Hint
Think about how multiplication and addition behave under modulo.
✗ Incorrect
Modular multiplication distributes over modular addition: (a * (b + c)) % m = ((a * b) % m + (a * c) % m) % m.
Modular addition is associative, so A is false. Modular subtraction can be negative before modulo, so C is false. Modular division is not always defined, so D is false.
🔧 Debug
expertIdentify the Error in Modular Inverse Calculation
Consider this code snippet that attempts to find the modular inverse of a number. What error will it produce when run?
DSA Javascript
function modInverse(a, m) {
for(let x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return null;
}
console.log(modInverse(6, 9));Attempts:
2 left
💡 Hint
Check if 6 has an inverse modulo 9.
✗ Incorrect
6 and 9 are not coprime, so modular inverse does not exist. The function returns null.
