Last Updated on July 4, 2023 by Mayank Dham
![]()
In the realm of mathematics, the sum of the first N natural numbers is a classic problem that has intrigued and challenged mathematicians for centuries. The concept of finding the sum of consecutive integers seems simple at first glance, but as N grows larger, the task becomes more complex. Thankfully, with the power and simplicity of Python, we can develop a program that efficiently calculates this sum, saving us precious time and effort.
In this article, we will explore the fascinating world of Python programming and unravel the mystery behind finding the sum of the first N natural numbers. Whether you’re a beginner taking your first steps into the world of programming or an experienced Python developer seeking to enhance your skills, this article will provide you with a comprehensive guide to solving this fundamental mathematical problem.
Approaches to Find the Sum of First N Natural Numbers
Here, we will discuss four different approaches to writing a Python Program to find the sum of first n natural numbers.
Approach 1: Python Program to find the sum of first n natural numbers Using Formula
Two natural numbers that follow one another have a difference of 1. The progression of natural numbers is an arithmetic progression since every pair of subsequent numbers has the same difference. An arithmetic progression goes as follows: a, a+d, a+2d, a+3d,…, where a is the first element of the number and d is the difference between two succeeding numbers in the progression.
The following formula can be used to determine the sum of n numbers in an arithmetic progression,
Sn=(n∗(2a+(n−1)∗d))/2
where,
The series’ first term is a.
The common difference between any two terms in a series is called d.
The series number of terms is n.
The preceding formula can be further condensed into the equation for the sum of natural numbers, (n(n+1))/2, by substituting a=1 and d=1 in the equation.
The number of elements in the sequence is represented by the symbol n in the equation.
Program to Calculate the Sum of Natural Numbers Using Formula
It is possible to use the Python Programming language to implement the formula given in the previous section.
Example
The formula for calculating the sum of natural numbers can be used to generate the following code,
Code Implementation
https://ideone.com/abf21c
Output
The Total Sum Of 10 Natural Numbers Is: 55
Note: Due to the precedence of the operator in C, brackets are used in the formula surrounding (natural_number+1). Multiplication and division operations will be carried out before addition in C because they have a higher priority than addition and subtraction operations. We employ a bracket that takes precedence over other operators to stop this from happening.
Explanation
The code variable natural_number serves as an example of the sequence’s element count. The user provides this variable as input. The sum is obtained by changing the formula for the sum of natural numbers to reflect the number of elements in the sequence. The user is shown the output.
The program’s time complexity is O(1), or continuous time complexity.
The program’s space complexity is also O(1).
Approach 2: Python Program to find the sum of first n natural numbers Using For Loop
The sum of natural numbers can be calculated by iterating through the numbers in the natural number sequence and adding them together using a for loop.
Example
There are two methods to use the for loop. The for loop in the program below iterates from 1 to a natural integer,
https://ideone.com/7sv7CH
The program below displays a for loop that iterates from the natural number to 1 (the first natural number in the sequence),
https://ideone.com/I0lFm2
Output
The total sum of 14 natural numbers is 105
Explanation
The user provides the highest possible number in the natural number series. Iterating from 1 to the greatest natural number or from the greatest natural number to 1 are both possible with a for loop. The total sum of natural numbers is obtained by adding the sum at each iteration.
Both programs will have an O(n) time complexity, where n is the number of iterations the for loop performs. Both programs’ space complexity will be O(1).
Approach 3: Python Program to find the sum of first n natural numbers Using While Loop
A while loop only checks for conditions from the second iteration of the loop’s run after running once without checking for the conditions it was given.
Example
https://ideone.com/7rmkWU
Output
The total sum of 13 natural numbers is 91
Explanation
The while loop multiplies the input number by each number starting at one. The user sees the outcome displayed.
The program has an O(n) time complexity, where n is the total number of iterations in the while loop. The program’s O(1) space complexity.
Approach 4: Python Program to find the sum of first n natural numbers Using Recursion
A technique known as recursion involves repeatedly calling the same function. Finding the sum of natural numbers can be accomplished using recursion rather than loops.
Example
https://ideone.com/7rwn4d
Output
The total sum of 12 natural numbers is 78
Explanation
The input consists of the natural number. In a recursive function call, the same function is invoked with the parameter set to the value obtained by subtracting the number supplied as an argument in the previous function call. Only if the number decreases to zero will the recursive call terminate. At the conclusion of each function call, the output that the function returned is summed to obtain the sum of natural numbers.
The recursive natural number addition has an O(n) time complexity. A recursive function’s space complexity is influenced by how many function calls are made. The space complexity in the aforementioned example is O(n).
The functions will be called in the following order: Sum(12) –> Sum(11) –> Sum(10)…–> Sum(1).
The function returns zero once Sum(0) is achieved, and the output of each function is then added together backward to determine the sum,
Sum(0) –> Sum(1) –> Sum(2) …–> Sum(12), etc.
The return function and the accumulation are both executed in the same phase. When the function is Sum(0), the result is 0, and when the function is Sum(1), the result is 0+1, where 0 is the result of the Sum(0) function. To obtain the sum of n natural numbers, analogous accumulations are carried out for each succeeding function.
Summary
Natural numbers are also known as counting numbers since they begin at one.
The natural number series follows an arithmetic progression, hence the sum of natural numbers can be calculated using the sum of natural numbers formula, which is derived from arithmetic progression.
Iterative methods utilizing for, while, or do..while loops can also be used to calculate the sum of natural numbers.
A recurrence function can also be used to determine the sum of natural numbers.
FAQ related to the Python Program to find the sum of first n natural numbers
Can we find the sum of first n natural numbers in O(1) time complexity?
Yes, there is one mathematical formula Sn=(n∗(2a+(n−1)∗d))/2, by using it we can calculate the sum of first n natural numbers. We had discussed this approach in this article.
Can the formula for finding the sum of the first n natural numbers be used for negative values of n?
A: No, the formula is valid only for positive values of n. It does not work for negative values of n.
What are the first n natural numbers?
The first n natural numbers are the positive integers starting from 1 and going up to n.