...

/

Solution: Nested Loop With Multiplication (Advanced)

Solution: Nested Loop With Multiplication (Advanced)

This review provides a detailed analysis of the different ways to solve the nested loop with multiplication challenge.

We'll cover the following...

Solution #

Press + to interact
n = 10 # can be anything
sum = 0
pie = 3.14
for var in range(n):
j = 1
while j < var:
sum += 1
j *= 2
print(sum)

In the main function, the outer loop is O(n)O(n) as it iterates over n. The inner while loop iterates over i which is always less than n and j is doubled each time, therefore we can say that it is O(log2(n))O(log_2(n)) ...

Access this course and 1400+ top-rated courses and projects.