Given the sequence of odd numbers
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, ….
Find the sum of first n odd numbers
Examples:
Input : n = 2
Output : 4
Sum of first two odd numbers is 1 + 3 = 4.
Input : 5
Output : 25
Sum of first 5 odd numbers is 1 + 3 + 5 +
7 + 9 = 25
A simple solution is to iterate through all odd numbers.
C++
#include <iostream>
using namespace std;
int oddSum( int n)
{
int sum = 0, curr = 1;
for ( int i = 0; i < n; i++) {
sum += curr;
curr += 2;
}
return sum;
}
int main()
{
int n = 20;
cout << " Sum of first " << n
<< " Odd Numbers is: " << oddSum(n);
return 0;
}
|
Java
import java.util.*;
class Odd
{
public static int oddSum( int n)
{
int sum = 0 , curr = 1 ;
for ( int i = 0 ; i < n; i++) {
sum += curr;
curr += 2 ;
}
return sum;
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println( " Sum of first " + n
+ " Odd Numbers is: " +oddSum(n));
}
}
|
Python3
def oddSum(n) :
sum = 0
curr = 1
i = 0
while i < n:
sum = sum + curr
curr = curr + 2
i = i + 1
return sum
n = 20
print ( " Sum of first" , n, "Odd Numbers is: " ,
oddSum(n) )
|
C#
using System;
class GFG {
public static int oddSum( int n)
{
int sum = 0, curr = 1;
for ( int i = 0; i < n; i++) {
sum += curr;
curr += 2;
}
return sum;
}
public static void Main()
{
int n = 20;
Console.WriteLine( " Sum of first " + n
+ " Odd Numbers is: " + oddSum(n));
}
}
|
PHP
<?php
function oddSum( $n )
{
$sum = 0; $curr = 1;
for ( $i = 0; $i < $n ; $i ++)
{
$sum += $curr ;
$curr += 2;
}
return $sum ;
}
$n = 20;
echo " Sum of first " , $n
, " Odd Numbers is: " , oddSum( $n );
?>
|
Javascript
<script>
function oddSum(n)
{
let sum = 0; curr = 1;
for (let i = 0; i < n; i++)
{
sum += curr;
curr += 2;
}
return sum;
}
let n = 20;
document.write( " Sum of first " + n
+ " Odd Numbers is: " + oddSum(n));
</script>
|
Output:
Sum of first 20 odd numbers is 400
Time Complexity: O(n)
Auxiliary Space : O(1)
An efficient solution is to use direct formula. To find the sum of first n odd numbers we can apply odd number theorem, it states that the sum of first n odd numbers is equal to the square of n.
∑(2i – 1) = n2 where i varies from 1 to n
let n = 10, therefore sum of first 10 odd numbers is
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100
if we apply odd number theorem:
sum of first 10 odd numbers = n * n = 10 * 10 = 100.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int oddSum( int n)
{
return (n * n);
}
int main()
{
int n = 20;
cout << " Sum of first " << n
<< " Odd Numbers is: " << oddSum(n);
return 0;
}
|
Java
import java.util.*;
class Odd
{
public static int oddSum( int n)
{
return (n * n);
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println( " Sum of first " + n
+ " Odd Numbers is: " +oddSum(n));
}
}
|
Python3
def oddSum(n) :
return (n * n);
n = 20
print ( " Sum of first" , n, "Odd Numbers is: " ,
oddSum(n) )
|
C#
using System;
class GFG {
public static int oddSum( int n)
{
return (n * n);
}
public static void Main()
{
int n = 20;
Console.WriteLine( " Sum of first " + n
+ " Odd Numbers is: " + oddSum(n));
}
}
|
PHP
<?php
function oddSum( $n )
{
return ( $n * $n );
}
$n = 20;
echo " Sum of first " , $n ,
" Odd Numbers is: " , oddSum( $n );
?>
|
Javascript
<script>
function oddSum(n)
{
return (n * n);
}
let n = 20;
document.write( " Sum of first " + n
+ " Odd Numbers is: " + oddSum(n));
</script>
|
Output:
Sum of first 20 odd numbers is 400
Time Complexity: O(1)
Auxiliary Space : O(1)
How does it work?
We can prove it using mathematical induction. We know it is true for n = 1 and n = 2 as sums are 1 and 4 (1 + 3) respectively.
Let it be true for n = k-1.
Sum of first k odd numbers =
Sum of first k-1 odd numbers + k'th odd number
= (k-1)*(k-1) + (2k - 1)
= k*k
Please Login to comment...