Solution: Nested Loop with Multiplication (Basic)
Understand how to evaluate time complexity for nested loops involving multiplication in Python. This lesson helps you calculate logarithmic and linear execution counts leading to the Big O notation O(n log n).
We'll cover the following...
We'll cover the following...
Solution #
Time Complexity
The outer loop in this problem, i.e., everything under line 5, while var < n runs times since var will first be equal to , then , then , until it is such that . This means that the outer loop runs a total of times. The inner loop, on the other hand, runs a total of . So,
| n = 10 | 1 |
| sum = 0 | 1 |
| pie = 3.14 | 1 |
| var = 1 | 1 |
| while var < n: |