0

Today in my computer science class the professor said: Time Complexity of addition is relative to the number of bits of the two numbers being added.

Does this mean an algorithm which goes through a list of numbers 1 to n(some input number), and computes the sum of the numbers in the list using a sum variable is actually O(n^2)? Because the time to add the numbers is relative to the number of bits n has?

I am confused because I feel like most people would say this is O(n). I don't know much about low level stuff yet so I am sorry if this is a dumb question.

Mark
  • 1
  • Are we talking about fixed-width integers (like C `int32_t`, Java `int`, etc.), or arbitrary-width integers (like GMP's `mpz_t`, Python's `int`, etc.)? – ShadowRanger Jan 19 '23 at 19:58
  • Arbitrary width. The other post answered my question so I will probably delete this one soon (we were actually going over a Fibonacci algorithm in class). But just wondering for like an interview or something, how do I know when the input can be an arbitrarily large number or not? Should I just assume the interviewer is talking about a fixed-width integer unless they specify? Does it depend on the language being used like Python vs Java? Sorry to bother you. – Mark Jan 19 '23 at 20:27
  • If they don't specify a language *and* a type within that language, just ask (sure, Python 3 doesn't really have a fixed-width type, but most other languages offer both, even if only via third-party library, as with C+GMP). Most interviewers I've dealt with aren't necessarily looking for the answer, they're looking for your thought process and depth of knowledge. Simply knowing there's a difference puts you ahead of most candidates they'll talk to (heck, a lot of programmers out there who didn't go through a four-year undergrad CS program have never even been exposed to the concept of big-O). – ShadowRanger Jan 19 '23 at 21:11

0 Answers0