r/badcode • • 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

Post image
1.2k Upvotes

80 comments sorted by

View all comments

Show parent comment

29

u/akren1 Jul 29 '21

Kind of picking on complexity here.

But python has built in big nums, so in theory time/space complexity is not O(1). It can be assumed to be for N in given range.

But it is still something to remember in the back of the mind.

0

u/[deleted] Jul 29 '21

[deleted] — view deleted 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.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted 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.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

1

u/WinstonCaeser Jul 30 '21

"There is nothing intrinsically linear about the runtime complexity of the mathematical calculation  x * (x + 1) / 2 . You have one input, no recursion, no looping, nothing. It operates in O(1). Theoretically speaking, of course."

There is, intrinsically, you must read every digit making it at least O(num digits).

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 30 '21

I keep saying digits, not saying the base. No matter what base you represent it in the size scales with the number. Be it base 2(binary) or base 264.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

→ More replies

1

u/akren1 Jul 30 '21

You've just agreed with me. I said what you did in your reasoning. But I said that given the context such assumption should be explicit. You didn't make it explicit. And just as machines that do Bignums in O(1), the ones that do not are also studied.

My point was that from the theoretical standpoint, things like BigNum don't exist- you only have numbers.

They exist - it's a hole field of both theoretical and practical study. Just as for example both theoretical and practical study of matrix multiplication.

From the theoretical standpoint BigNums exist just like matrices do. You either analyze number in given range, or arbitrarily large. And when you analyze arbitrarily large numbers you need some assumptions.

Again, that's the point I've been making. If your STL hashmap does its lookups in linear time, you'll never reap the benefits of an actual O(1) lookup. However, that doesn't mean hashmaps don't have a theoretical O(1) lookup, it means there's an implementation quirk to be aware of in that language.

Bignums don't have theoretical O(1) arithmetic - it can be proven pretty easily.

There is nothing intrinsically linear about the runtime complexity of the mathematical calculation x * (x + 1) / 2. You have one input, no recursion, no looping, nothing. It operates in O(1). Theoretically speaking, of course.

Just as you don't see looping in std::fill. But it is there - it is just hidden by pretty operator. Maybe it is not intrinsic, but that's why there are entire theories/algorithms about it.

Again- your point was never wrong, you just mislabeled the theoretical limit portion.

Neither is yours in logical way. This more of an "definitions" dispute. But with that in mind my claims stand unchanged.

6

u/WinstonCaeser Jul 30 '21 edited Jul 30 '21

It depends if you consider a number its own thing, or a series of digits in your "theory".

Edit: Theoretically, you must read the whole input, this obviously scales with the number of digits.

If you bound yourself to numbers of under a certain size, than obviously you can read the whole size every time, but then your algorithm has artificial limits. You continue to base your analysis on these artificial limits.

Algorithms such as std::fill are impossible to be reduced to lower than O(n), as it obviously must visit every element and set it's value. There is no "language update" that can reduce an algorithm below it's theoretical complexity without limiting the inputs.

"There is a trivial lower bound of Ω(n) for multiplying two n-bit numbers on a single processor; no matching algorithm (on conventional machines, that is on Turing equivalent machines) nor any sharper lower bound is known."

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 30 '21

My CS professors specifically taught me different multiplication algorithms and expected me to implement them, including grade-school, karatsuba, toom-cook, and finally I learned about, but did not implement Schönhage–Strassen. We discussed at length when to consider the number to be itself a single unit, or a series of digits.

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 30 '21

I'm not assuming any underlying implementation, you are.

You are assuming that numbers are capped at some value roughly 264, which is very much an assumption. Because this is in python that assumption is particularly wrong since it has no such limitation.

It is simply a question of what you define as the base "unit", is it a digit, or is it a entire number. You are assuming that the base is a whole number, where I am pointing out that it could also be a digit. It has nothing to do with how it is implemented.

Regardless of implementation any general case multiplication is at least O(num digits).

https://en.m.wikipedia.org/wiki/Multiplication_algorithm#Lower_bounds

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

0

u/WinstonCaeser Jul 30 '21 edited Jul 30 '21

For any Turing machine (the theoretical computer) multiplication is at least O(num digits). On any machine to multiply two numbers of unbounded size it takes at least O(num digits).

You need to brush up on your complexity theory if you are going to try and be pedantic.

You make keep making the same assumption about the architecture, that numbers are upper bounded. This is in many cases an invalid assumption and must be explicitly stated.

How can it not affect the algorithmic time complexity? Whether the assumption about upper limits is made changes (n *( n+1))/2 from O(1) to at least O(n). Where n is the number of digits.

Edit: Once again linking Wikipedia in hopes that you'll read it and stop embarrassing yourself.

Relevant quote: "There is a trivial lower bound of Ω(n) for multiplying two n-bit numbers on a single processor; no matching algorithm (on conventional machines, that is on Turing equivalent machines) nor any sharper lower bound is known."

https://en.m.wikipedia.org/wiki/Multiplication_algorithm#Lower_bounds

0

u/[deleted] Jul 30 '21

[deleted] — view deleted comment

→ More replies

1

u/AlarmingAffect0 Jul 30 '21

Like David S. Pumpkins?

9

u/[deleted] Jul 29 '21

Shit! you’re right.

I was thinking in number variable allocated and number of operations, but if we take into account real cpu ops its more than O(1)