Python programs to find the sum of the series $1 + 2 + 3 + ... + n$ i.e. the sum of the first $n$ natural numbers have been shown here. Both the iterative and recursive approaches have been shown below. The sum can also be computed using the mathematical formula $\frac{n(n+1)}{2}$.
Example:
Suppose $n = 7$
So the sum of first $7$ natural numbers = $1 + 2 + 3 + 4 + 5 + 6 + 7 = 28$
By using the mathematical formula, we obtain the sum of first $7$ natural numbers = $\frac{7 \cdot (7+1)}{2} = 28 $
The algorithm, pseudocode and time-complexity of the program have also been covered in the following sections.
Page content(s):
1. Algorithm to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
1. Take number of terms $n$ as input.
2. Set $sum = 0$.
3. Add each $i \in [1, n]$, to $sum$.
4. Display $sum$ as output.
2. Pseudocode to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
Input: Number of terms $n$
Output: Sum of $1+2+3+...+n$
1. Procedure sumSeries($n$):
2.
3.
4.
5.
6. End Procedure
3. Time complexity to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
Time Complexity: O(n)
Here $n$ is the number of terms.
Time Complexity (Using formula): O(1)
4. Program & output to find the sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
4.1. Python Program & output to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using formula
# *********************************************** # alphabetacoder.com # Python program to find sum of series 1 + 2 + 3 # +...+n using the formula n(n + 1) / 2 # *********************************************** # initialize s = 0 # take no of terms as input n = int(input("Enter no of terms: ")) # calculate the sum using formula s = (n * (n + 1)) // 2 # display result print("Sum =", s)
Output
Enter no of terms: 75
Sum = 2850
4.2. Python Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using iteration
# *********************************************** # alphabetacoder.com # Python program to find sum of series 1 + 2 + 3 # +...+n using iterative approach # *********************************************** # initialize s = 0 # take no of terms as input n = int(input("Enter no of terms: ")) # calculate the sum using loop for i in range(1, n + 1): s = s + i # display result print("Sum =", s)
Output
Enter no of terms: 30
Sum = 465
4.3. Python Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using recursion
# *********************************************** # alphabetacoder.com # Python program to find sum of series 1 + 2 + 3 # +...+n using iterative approach # *********************************************** # recursive function to calculate sum # of series 1 + 2 + 3 ... + n def calculate_sum(n): # exit condition of recursive call if n == 0: return 0 # call function return n + calculate_sum(n - 1) # declare a main function def main(): # take no of terms as input n = int(input("Enter no of terms: ")) # call recursive function # to calculate sum # display the result print("Sum =", calculate_sum(n)) # driver code main()
Output
Enter no of terms: 100
Sum = 5050