 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of square of first n odd numbers
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 − 12, 32, 52, 72, 92, 112….
The sum of this series has a mathematical formula −
n(2n+1)(2n-1)/ 3= n(4n2 - 1)/3
Lets take an example,
Input: N = 4 Output: sum =
Explanation
12 + 32 + 52 + 72 = 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.
Example
#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; } Output
The sum of square of first 8 odd numbers is 680
Example
#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; } Output
The sum of square of first 18 odd numbers is 7770
Advertisements
 