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
5
u/akren1 Jul 30 '21 edited Jul 30 '21
I think I see your point, but I don't really agree.
I was referring to the "theory" as a "non practicall use".
What I believe you understand as a theory is complexity theory abstracted out of Python, or any given language. But even in such abstraction it is not O(1) when you consider bugnums (don't limit domain is N).
So it is not O(1) for domain of all natural numbers which is valid domain.
In Python the + or * operation are simply not an single operations.
Taking all dynamic typing out of the way, and just focusing on integers it is a function call.
And if we don't limit domain of n, that function tme/space complexity if not O(1). I believe a+b is O(log ab), and * can depend on used algorithm.
It can by O(1) if we constraint N by int32, int64, or even int_21000. But for arbitrarily large numbers complexity depends on a number, not just amount of them. Given the python code I would assume such numbers even if we abstract out of python.