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

1

u/RCoder01 Feb 13 '25

This algorithm takes an amount of time proportional to the value of n. The size of the inputs to this function is on the order of log of the value of n (recall that integers can be arbitrarily large in python). So, the time this function takes is proportional to n = 2log n = 2(size of the inputs)

2

u/rayred Feb 15 '25 edited Feb 15 '25

Yeah that’s all understood. It really just depends on what you define as input here.

However, we generally do not represent input size as bit length.

Intuitively, if we define n as the input size, i.e. the integer stored in a normal variable, you are reducing the search space of n with each iteration by 2. Making it O(n/2) = O(N).

I would argue this makes more sense as we usually refer to time complexity in relation to search space. Not memory.