-5

f(n)=5/n;

What is the BigOh of f(n)?

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73

2 Answers2

3

The complexity does not grow with n, thus O(1)

Grogi
  • 2,099
  • 17
  • 13
2

The time complexity of computing f(n) is O(1), and the space complexity is either O(1) or zero (depending on whether you count temporary registers as space)1.

If (hypothetically) f(n) was a cost function2 for a computation, then its complexity class would be O(1/n). However, that makes no sense3. How can you possibly have a cost function that tends towards zero as N tends towards infinity? The cost will either be zero (for a null computation, or no space), or at least 1. Costs are measured as measured as multiples of some indivisible unit; e.g. bits or bytes or instructions or clock cycles.


1 - Actually, this is an over-simplification. The space required to represent 5/n precisely as floating point number (in a given base) is infinite. Therefore the time to compute that number representation precisely for those n values is also infinite. However, that is not the way we normally write programs. Computing the precise value of (for example) 5 / 3 in decimal is a fundamentally pointless exercise.

2 - For example, the time taken to perform the computation, the space taken by the computation ... or some other cost of performing a computation.

3 - It does make sense mathematically, according to the definition of Big Oh notation.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216