- Related Questions & Answers
- Sum of square-sums of first n natural numbers
- Average of first n odd naturals numbers?
- Difference between sum of the squares of and square of sum first n natural numbers.
- PHP program to calculate the sum of square of first n natural numbers
- Squared sum of n odd numbers - JavaScript
- Program to find the sum of first n odd numbers in Python
- Sum of the first N Prime numbers
- Sum of sum of first n natural numbers in C++
- Sum of first n natural numbers in C Program
- Sum of Square Numbers in C++
- C++ Program for Sum of squares of first n natural numbers?
- Sum of squares of first n natural numbers in C Program?
- Python Program for Sum of squares of first n natural numbers
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The series of squares of first n odd numbers takes squares of of first n odd numbers in series.

The series is: 1,9,25,49,81,121…

The series can also be written as − 1^{2}, 3^{2}, 5^{2}, 7^{2}, 9^{2}, 11^{2}….

The sum of this series has a mathematical formula −

n(2n+1)(2n-1)/ 3= n(4n^{2} - 1)/3

Lets take an example,

Input: N = 4 Output: sum =

12 + 3^{2} + 5^{2} + 7^{2} = 1 +9+ 25 + 49 = 84

Using formula, sum = 4(4(4)^{2}- 1)/3 = 4(64-1)/3 = 4(63)/3 = 4*21 = 84 both these methods are good but the one using mathematical formula is better because it does not use looks which reduces its time complexity.

#include <stdio.h> int main() { int n = 8; int sum = 0; for (int i = 1; i <= n; i++) sum += (2*i - 1) * (2*i - 1); printf("The sum of square of first %d odd numbers is %d",n, sum); return 0; }

The sum of square of first 8 odd numbers is 680

#include <stdio.h> int main() { int n = 18; int sum = ((n*((4*n*n)-1))/3); printf("The sum of square of first %d odd numbers is %d",n, sum); return 0; }

The sum of square of first 18 odd numbers is 7770

Advertisements