educative.io

Calculation time complexity

n = 10 # just as an example, n can be anything
sum = 0
for var in range(n):
sum += 1

print(sum)

var=0 1
var=1 1
var=2 1
var=n-1 1

For the calculation of time complexity of above example…as the value of n is 10 so the var will be calculated for 0,1,2,3,4,5,6,7,8,(n-1) or 9 times ,in that case the time complexity would be 1 for each operation which will leads for 1+1+1+1+1+1+1+1+1+1= 10,but how it could be 1+1+1…1 = 4 ,could you please explain this process?


Course: Algorithms for Coding Interviews in Python - Learn Interactively
Lesson: Example 1: Measuring Time Complexity - Algorithms for Coding Interviews in Python

Hello @Timir_E

As you have mentioned above “how it could be 1+1+1…1 = 4” It’s a mathematical convention “…” that represents the continuation of anything. In this case, we also meant the same thing as you have mentioned above “1+1+1+1+1+1+1+1+1+1= 10”.

We followed the convention, like that “1+1+1+1…1”

When we write “1+1+1+1…+1”, the ellipsis “…” is used as a shorthand notation to represent a sequence of repeated terms. In this case, the sequence is a series of 1’s added together.

When we write “a, b, c, …, x, y, z”, the ellipsis “…” is used to represent all the letters between “a” and “z” in alphabetical order. This is a common way to denote a sequence of items that follow a certain pattern or order.

I hope this will help.
Happy Learning.

1 Like