ProgrammingHard
For a matrix of size n x n with n = 500, which algorithm is correct and fast enough within 1 second for C multiplication?
- A. Naive loops with four nested levels, about 6 x 10^10 steps
- B. Naive triple loop, about 1.25 x 10^8 multiply-adds
- C. Recursive exponent loops that repeat each product n times, about 10^12 steps
- D. Multiplying every pair of rows, about 5 x 10^10 steps
Correct answer: B. Naive triple loop, about 1.25 x 10^8 multiply-adds
The standard triple loop does n^3 = 1.25 x 10^8 multiply-adds, which fits comfortably in a second in C. Any O(n^4) scheme gives about 6 x 10^10, far too slow.
More C Programming questions
- Given int a[3][4] and int q = &a[0][0], which element does q[5] refer to?
- int m[2][3] = {{1}, {2, 3}}; What is m[0][0] + m[1][0] + m[1][1] + m[0][2]?
- int a[3][4]; what do sizeof a / sizeof a[0] and sizeof a[0] / sizeof a[0][0] give?
- char g[3][4] = {"abc", "de", "f"}; What is the value of g[1][2] when printed with %d?
- int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; How many monotone paths (moves right or down) are there from m[0][0] to m[2][2]?
- For the grid w = {{1,3,1},{1,5,1},{4,2,1}}, what is the minimum sum of a path from top-left to bottom-right moving only right or down?