...
/Solution: Nested Loop with Multiplication (Basic)
Solution: Nested Loop with Multiplication (Basic)
This review provides a detailed analysis of the different ways to solve the nested loop with multiplication problem.
We'll cover the following...
Solution #
Press + to interact
Python 3.5
n = 10 # n can be anything, this is just an examplesum = 0pie = 3.14var = 1while var < n:print(pie)for j in range(1, n, 2):sum += 1var *= 3print(sum)
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: |
Ask