r/badcode • u/zshadowjon • Jul 29 '21
python Was doing some bug hunting and found essentially what was a sum to n function but of O(fuck) time complexity
1.2k
Upvotes
r/badcode • u/zshadowjon • Jul 29 '21
View all comments
Show parent comment
0
u/WinstonCaeser Jul 31 '21
Bruh, idk how many times I have to explain this to you.
As I said from the very beginning, it depends if your base unit is a number or a more precisely a digit.
INTRINSICALLY you must read all digits, therefore it is At least O(num digits) for any number, you must read and operate on each of its digits.
You are stuck on evaluating the implementation that most architectures limit multiplication to a fixed number of digits and operate assuming worst case.
In THEORY multiplication is at least O(num digits). In practice, many implementations limit multiplication to a fixed number of digits making it O(1).
It is often extremely important when operating with matrices to consider the cost of multiplication.
Particularly for simple functions, like the above, where the cost of multiplication is not dominated by some other part of the function, it becomes important to factor in the cost.