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)); }- A. 5 24
- B. 3 1024
- C. 1 24
- 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
- What is printed? c #define STR(x) #x printf("%zu", strlen(STR( a + b )));
- What is printed? c char g[3][4] = {"abc", "def", "ghi"}; for (int j = 0; j < 3; j++) putchar(g[2 - j][j]);
- A file report.txt already exists and holds data. What does fopen("report.txt", "w") do?
- The user types hello and presses Enter. After char buf[8]; fgets(buf, sizeof buf, stdin);, what does buf hold?
- The user types abcdefghij and presses Enter. What is strlen(buf) after char buf[8]; fgets(buf, sizeof buf, stdin);?
- After scanf("%d", &n); reads the input 42 followed by Enter, why does the next getchar() return 'n' instead of waiting for new input?