ProgrammingHardProduct company OA

What is printed?

What is printed?
int pm(int b, int e, int m) {
    if (e == 0) return 1 % m;
    int h = pm(b, e / 2, m);
    h = h * h % m;
    return e % 2 ? h * b % m : h;
}
int main(void) { printf("%d %d", pm(3, 13, 7), pm(2, 10, 1000)); }
  1. A. 5 24
  2. B. 3 1024
  3. C. 1 24
  4. D. 3 24

Correct answer: D. 3 24

pm computes b^e mod m by repeated squaring. 3^6 ≡ 1 (mod 7), so 3^13 = (3^6)^2 · 3 ≡ 3. 2^10 = 1024 ≡ 24 (mod 1000). Reducing after each multiplication keeps the numbers small.

More C Programming questions

Exams that ask this topic