f(n)=5/n;
What is the BigOh of f(n)?
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.