Suppose we have one number n, we have to find the sum of the first n positive odd numbers.
So, if the input is like 7, then the output will be 49 as [1+3+5+7+9+11+13] = 49
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): if n == 0: return 0 sum = 1 count = 0 temp = 1 while(count<n-1): temp += 2 sum += temp count += 1 return sum ob = Solution() print(ob.solve(7))
7
49