Given a positive integer n, find the sum of the first n natural numbers.
Examples :Â
Input: n = 3
Output: 6
Explanation: 1 + 2 + 3 = 6Input: n = 5
Output: 15
Explanation: 1 + 2 + 3 + 4 + 5 = 15
Table of Content
Sum of Natural Numbers is represented by Triangular Numbers.
[Naive Approach] Loop - O(n) Time and O(1) Space
The idea is to initialize sum to 0 and iterate from 1 to n, adding each number to sum. After the loop ends, return sum.
Consider n = 4
- Initially : sum = 0
- i = 1, sum = sum + 1 = 1
- i = 2, sum = sum + 2 = 3
- i = 3, sum = sum + 3 = 6
- i = 4, sum = sum + 4 = 10
#include <iostream>
using namespace std;
int findSum(int n){
int sum = 0;
// iterating over all the numbers
// between 1 to n
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
return sum;
}
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
#include <stdio.h>
int findSum(int n){
int sum = 0;
// iterating over all the numbers
// between 1 to n
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
return sum;
}
int main()
{
int n = 5;
printf("%d", findSum(n));
return 0;
}
class GFG{
static int findSum(int n){
int sum = 0;
// iterating over all the numbers
// between 1 to n
for (int i= 1; i <= n; i++)
{
sum = sum + i;
}
return sum;
}
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
def findSum(n):
sum = 0
i = 1
# Iterating over all the numbers between 1 to n
while i <= n:
sum = sum + i
i = i + 1
return sum
if __name__ == "__main__":
n = 5
print(findSum(n))
using System;
class GFG{
static int findSum(int n)
{
int sum = 0;
// iterating over all the numbers
// between 1 to n
for (int i = 1; i <= n; i++)
{
sum = sum + i;
}
return sum;
}
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
function findSum(n)
{
let sum = 0;
// iterating over all the numbers
// between 1 to n
for (let i = 1; i <= n; i++)
{
sum = sum + i;
}
return sum;
}
// Driver Code
let n = 5;
console.log(findSum(n));
Output
15
[Better Approach] Recursion -O(n) and O(n) Space
We use recursion to find the sum of the first n natural numbers.
- The function calls itself with (n-1) until it reaches the base case of n = 1.
- Each call adds the current value of n to the sum of smaller values.
#include <iostream>
using namespace std;
int findSum (int n){
// base condition
if (n == 1 )
return 1 ;
return n + findSum(n - 1);
}
int main() {
int n = 5 ;
cout << findSum(n);
return 0;
}
public class GFG {
static int findSum(int n) {
// base condition
if (n == 1)
return 1;
return n + findSum(n - 1);
}
public static void main(String[] args) {
int n = 5;
System.out.println(findSum(n));
}
}
def findSum(n):
# base condition
if n == 1:
return 1
return n + findSum(n - 1)
if __name__ == "__main__":
n = 5
print(findSum(n))
using System;
class GFG
{
static int findSum(int n)
{
// base condition
if (n == 1)
return 1;
return n + findSum(n - 1);
}
static void Main()
{
int n = 5;
Console.WriteLine(findSum(n));
}
}
function findSum(n) {
// base condition
if (n == 1)
return 1;
return n + findSum(n - 1);
}
//Driver code
let n = 5;
console.log(findSum(n));
Output
15
[Expected Approach] Formula Based Method- O(1) Time and O(1) Space
The sum of the first n natural numbers can be calculated directly using the formula: (n * (n+1)) / 2
We can prove this formula using mathematical induction.
Base Case:
- For n = 1,
- Sum = 1 * (1 + 1) / 2 = 1
- Thus, the formula holds for (n = 1).
Induction Hypothesis:
- Assume the formula is true for n = k, i.e.,
- Sum of first k numbers = k * (k + 1) / 2
Induction Step:
- Putting k = n - 1, we get
- Sum of first n - 1 numbers = (n - 1) * n / 2
- If we add n to this sum, we get,
Sum of first n numbers = n + (n - 1) * n / 2
= (2n + n2 - n) / 2
= n * (n + 1) / 2
Hence, the formula is proved.
#include<iostream>
using namespace std;
int findSum(int n)
{
// Using mathematical formula to compute
// sum of first n natural numbers
return n * (n + 1) / 2;
}
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
#include<stdio.h>
int findSum(int n)
{
// Using mathematical formula to compute
// sum of first n natural numbers
return n * (n + 1) / 2;
}
int main()
{
int n = 5;
printf("%d", findSum(n));
return 0;
}
class GFG{
static int findSum(int n)
{
// Using mathematical formula to compute
// sum of first n natural numbers
return n * (n + 1) / 2;
}
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
def findSum(n):
# Using mathematical formula to compute
# sum of first n natural numbers
return n * (n + 1) // 2
if __name__ == "__main__":
n = 5
print(findSum(n))
using System;
class GFG
{
static int findSum(int n)
{
// Using mathematical formula to compute
// sum of first n natural numbers
return n * (n + 1) / 2;
}
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
function findSum(n)
{
// Using mathematical formula to compute
// sum of first n natural numbers
return n * (n + 1) / 2;
}
// Driver Code
var n = 5;
console.log(findSum(n));
Output
15