Back to problems
COMP2521
Complexity: Multiplications in Matrix Multiply
Complexity: scalar multiplications in N×N matrix multiply
Multiplying two N×N matrices with the naive triple-loop algorithm: how many scalar multiplications does it do? Read N and print that count.
Complete matMultCount.
Key derivation: the result matrix hasN×Nentries; each entry is a row-dot-column needingNmultiplications. So the total isN × N × N = N³. That is the origin of matrix multiply's O(n³). ForN = 3it is27(the answer to 21T3 Q1).
Input
- One line: integer
N(0 ≤ N ≤ 1000).
Output
- One line: the number of scalar multiplications,
N³.
Example: input 3 -> 27.
Samples
stdin
3
expected
27
stdin
2
expected
8
c-dcc
14