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
1
u/akren1 Jul 30 '21 edited Jul 30 '21
But you can't analyse algorithms with arbitrarily large numbers as inputs, without includind algorithms for arithmetics operation into consideration. This is not an implementation detail. This is computational theory of algorithms that work with bignums. And there are a lot of algorithms that are known in this field.
What if instead of a+b we write: BignumAdd(a, b). You can assume it is O(1), but such algorithms does not exist. You can mark it as unknown - f(a,b) and analyse from there if you want to abstract out of "implementation details". But that is no implementation, that is bignum arithmetics.
Abstrat machines that can do such operations in O(1) are theoretically studied, and you can assume we work with one like that. But given the context such assumption should be explicit.
And on the side note. If I analyse time complexity on a given runtime, it does depend on the runtime. Built in functions/features have it's complexity. And the complexity might be different on different runtime.
Edit:
It's like saying: std::fill is O(1) when we analyse time complexity of C++ program - it's just not. And we must assume something when we analyse complexity of entrie algorithm.
What if stl got an update next week that makes std::fill run in O(1) - it would change complexity of our algorithm.