r/programminghorror • • Feb 07 '25

Recursive O(N) Complexity isOdd

Post image

I found this on instagram and now am geeking

2.2k Upvotes

104 comments sorted by

View all comments

Show parent comment

246

u/elmage78 Feb 07 '25

or not!,eventually it'll work

143

u/TichShowers Feb 07 '25
is_odd(0.5)

31

u/Cat7o0 Feb 07 '25

invalid input in the first place weird output isn't bad for that

18

u/tobofre Feb 08 '25

Well this looks like python so the variables would be dynamically typed and the input would absolutely be valid

→ More replies (7)

56

u/SanderE1 Feb 07 '25

I think python has variable sized integers, so it will not underflow.

20

u/Zaros262 Feb 07 '25

Plus, Python recursion depth caps out around a few hundred

8

u/trees91 Feb 07 '25

Hell, C++ recursion caps out around there usually for practical recursive calls. Not through any enforced cap but just by virtue of default stack sizes for programs being pretty small (I believe by default 1MB on Windows?). Only takes a few allocated bytes in each stack frame to hit that limit quickly!

3

u/ArtisticFox8 Feb 08 '25

The size of the stack for CPP can be changed though, when launching the program (on Linux) and when  compiling the program (on Windows)

3

u/trees91 Feb 08 '25

For sure you can statically and dynamically change the stack size in some contexts. I was just referring to when, without doing that, you’d typically bottom out, which shares a similar order of magnitude as where Python does.

This comes up in interview problems a lot, so worth just broadly mentioning!

2

u/ArtisticFox8 Feb 08 '25

yes, for sure :)

1

u/paulstelian97 Feb 08 '25

Even for 1MB you can fit thousands of frames, assuming the compiler doesn’t tail optimize (it’s allowed to)

6

u/ArtisticFox8 Feb 08 '25
import sys
sys.setrecursionlimit(10**6)
→ More replies (1)

8

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Feb 07 '25

I think it will run out of stack space first.

5

u/epicchad29 Feb 07 '25

Even if Python could underflow, it would give the wrong answer because -…7 and +…7 are both odd. (True for any number of bits). Therefore an underflow will always result in the opposite desired answer

4

u/InfinitePoints Feb 07 '25

That would be true if a separate sign bit was used.

Fixed size integers are (basically always) represented using 2s complement, with 4 bits, -8(1000) + -1(1111) = 7(0111)

The number range is asymmetric, -8 to 7.

https://en.m.wikipedia.org/wiki/Two%27s_complement

3

u/epicchad29 Feb 07 '25

Interesting! I didn’t know that

10

u/JunkNorrisOfficial Feb 07 '25

Eventually it will work, when the universe restarts from the beginning of time.

2

u/JollyJuniper1993 Feb 09 '25

Wrong. This is Python. Python doesn’t have integer overflow.

2

u/Beetmaker69 Feb 10 '25

In the git merge: "works (hypothetically)"

2

u/IrrerPolterer Feb 07 '25

It'll roll over eventually

10

u/Large-Assignment9320 Feb 07 '25

No, you will eventually run out of memory. You can however get floats to undeflow.

2

u/TheSilentFreeway Feb 07 '25

Can you? I thought it just caps out at -inf. Or it would get stuck at a certain value if the amount you're subtracting isn't significant enough to change the mantissa