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/WinstonCaeser Aug 01 '21 edited Aug 01 '21
Your clearly do not understand even basic complexity. As demonstrated by not even knowing Big Omega notation, when it was directly linked on the page, or the concept of how we calculate complexity (spoiler: it's with well defined models of computation, we don't just make up how long it takes to do things and randomly assume things to be O(1))
When analyzing algorithms knowing what machine is being operated on is essential to analyzing the complexity of an algorithm to have a baseline, as any intro to complexity lecture will tell you.
"The evaluation of the complexity relies on the choice of a model of computation, which consists in defining the basic operations that are done in a unit of time. When the model of computation is not explicitly specified, this is generally meant as being multitape Turing machine."
https://en.wikipedia.org/wiki/Computational_complexity#Models_of_computation
If you continue to just makeup random baselines, your analysis of complexity is meaningless. I could say that I have a computer that can multiply matrices numbers in O(1), but that would have to be clearly stated in my assumptions before doing analysis. (Because it is a deviation from the standard, and generally a terrible assumption, unless analyzing something truly weird and unique).
Arithmetic is only sometimes elided, as I said from the very beginning it depends whether your base is a number or the digits that make it up.
Note the below quote on how arithmetic complexity is only used if you have an upper bound on the input. Have fun eating dirt.
From Wikipedia: "The number of arithmetic operations is another resource that is commonly used. In this case, one talks of arithmetic complexity. If one knows an upper bound on the size of the binary representation of the numbers that occur during a computation, the time complexity is generally the product of the arithmetic complexity by a constant factor.
For many algorithms the size of the integers that are used during a computation is not bounded, and it is not realistic to consider that arithmetic operations take a constant time. Therefore, the time complexity, generally called bit complexity in this context, may be much larger than the arithmetic complexity."
https://en.wikipedia.org/wiki/Computational_complexity#Resources
"In the abstract arithmetic is free" This makes no sense, if discussing the cost of calculating stuff, you can't just go willy-nilly assuming whatever you feel like to be free. To analyze complexity, you must have a theoretical machine doing operations. If your abstraction can calculate anything instantaneously, sure, but then analyzing the complexity in your fairy land is meaningless, and should be clearly stated in your assumptions as it is a deviation from the standard, particularly since you insist you aren't bounding the input, making it a deviation from what anyone in the field uses and particularly meaningless.
What do you mean not digits? I have continuously said digits, not base 10, not base 2, not base 264, I said digits, which is generic to however any implementation (not made up by you) operates.
Edit: switched to desktop links from mobile ones
TLDR:
Complexity of an algorithm cannot be modeled without a model of computation, if one is not specified it is assumed to be a Multitape Turing Machine.
The cost of arithmetic operations can only be ignored if operating on an upper bounded input size, otherwise it is not realistic to consider that arithmetic is constant time.