Table of Contents
Explanation
Example
Output
Home Backend Development C++ The sum of the squares of the first n odd numbers

The sum of the squares of the first n odd numbers

Aug 31, 2023 pm 08:29 PM
odd number sum of squares first n

The sum of the squares of the first n odd numbers

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

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

The series can also be written as- 12, 32 , 52, 72, 9 2, 112….

The sum of this series is A mathematical formula-

n(2n 1) (2n-1)/ 3= n(4n2 - 1)/3

For example,

Input: N = 4
Output: sum =
Copy after login

Explanation

12 32 52 72 = 1 9 25 49 = 84

Using the formula, sum = 4(4(4)2- 1)/3 = 4(64-1)/3 = 4(63)/3 = 4*21 = 84 Both methods is good, but the method using mathematical formulas is better because it doesn't use looks, thus reducing the 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;
}
Copy after login

Output

The sum of square of first 8 odd numbers is 680
Copy after login

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;
}
Copy after login

Output

The sum of square of first 18 odd numbers is 7770
Copy after login

The above is the detailed content of The sum of the squares of the first n odd numbers. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The sum of the squares of the first n odd numbers The sum of the squares of the first n odd numbers Aug 31, 2023 pm 08:29 PM

The series of squares of the first n odd numbers takes the square of the first n odd numbers in the 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)/3For example, Input:N=4Output:sum=Explanation 12+32+52+72=1+9+25+49=84 Using the formula, sum=4 (4(4)2-1)/3=4(64-1)/3=4(63)/3=4*21=84 Both methods are good, but the method using mathematical formulas is better , which reduces the time complexity since it does not use a look

C program to print 'even' or 'odd' without using conditional statements C program to print 'even' or 'odd' without using conditional statements Sep 15, 2023 pm 09:21 PM

In this section, we will see how to check whether a number is odd or even without using any conditional statements like <, <=, !=, >, >=, ==. We can easily check if the number is odd or even by using conditional statement. We can divide the number by 2 and check if the remainder is 0. If 0, it is an even number. Otherwise, we can AND the number with 1. If the answer is 0, it is an even number, otherwise it is an odd number. Conditional statements cannot be used here. We'll see two different ways to check whether an odd or even number is present. Method 1 Here we will create an array of strings. The index 0 position will hold "even" and the index 1 position will hold "odd". We can divide numbers

Given an odd number, find the average of all odd numbers Given an odd number, find the average of all odd numbers Sep 03, 2023 pm 03:49 PM

The average of odd numbers up to a given odd number is a simple concept. You just need to find the odd numbers up to that number, then add them and divide by that number. If you want to find the average of odd numbers up to n. Then we will find the odd numbers from 1 to n and add them together and divide by the number of odd numbers. Example The average of odd numbers up to 9 is 5, i.e. 1+3+5+7+9=25=>25/5=5 There are two ways to calculate the average of odd numbers up to n, where n is an odd number using a loop using the formula Program to find the average of odd numbers up to n, using a loop. To find the average of odd numbers up to n, we will add all the numbers up to n and divide by the number of odd numbers up to n. Program to calculate average of odd natural numbers up to n - example code

Sum of squares of first n even numbers in C program Sum of squares of first n even numbers in C program Sep 12, 2023 pm 03:57 PM

Sum of squares of first n even numbers means, we first find the squares and add them all to get the sum. There are two ways to find the sum of the squares of the first n even numbers using a loop. We can use a loop to iterate from 1 to n, incrementing by 1 each time, find the square and add it to the sum variable − Example #include<iostream>usingnamespacestd;intmain (){ intsum=0,n=12; for(inti=1;i<=n;i++) &nb

The product of N and the largest odd number of digits in C The product of N and the largest odd number of digits in C Aug 29, 2023 pm 01:25 PM

GivenanumberNwithwehavetoproductthenumberwithitslargestodddigit.Ifthereisnoodddigitthenprint-1.LikewehaveinitializedNwith“153”andthelargestodddigitinthisnumberis5sotheresultwouldbetheproductof153with5i.e.153*5=765andifthenumberhas

The sum of the square sums of the first n natural numbers The sum of the square sums of the first n natural numbers Sep 09, 2023 am 11:53 AM

The sum of the squares of the first n natural numbers is the sum of the squares of up to n terms. This series finds the sum of every number up to n and adds the sum to the sum variable. The sum of the square sums of the first four natural numbers is -sum=(12)+(12+22)+(12+22+32)+(12+22+32+42)=1+5+14+30=50 There are two ways to find the sum of the squares of the first n natural numbers. 1) Use a for loop. In this method we will loop through each number from 1 to N and find the sum of squares and then add this sum of squares to the sum variable. This method requires iteration over n numbers so will be very time consuming for larger numbers. Example#include<stdio.h>i

XOR query for maximum odd divisor in range in C++ XOR query for maximum odd divisor in range in C++ Aug 27, 2023 pm 08:25 PM

Given an array of N integers and Q range queries. For each query, we need to return the XOR of the largest odd divisor of each number in the range. The largest odd divisor is the largest odd number that can divide the number N, e.g. For example, the largest odd divisor of 6 is 3. Input:nums[]={3,6,7,10},query[]={{0,2},{1,3}}Output:query1:7query2:1Explanation:greatestodddivisorsofnumsarrayare{3,3,7,5 }.Forquery1weneedtofindtheXORofindexes0,

PHP implementation tips: Get odd numbers within 100 PHP implementation tips: Get odd numbers within 100 Mar 09, 2024 pm 02:36 PM

PHP is a popular server-side programming language widely used for website development and dynamic web page generation. In PHP, getting odd numbers within a certain range is a common need. This article will introduce how to use PHP to obtain odd numbers within 100, and provide specific code examples. First, we can use a for loop to iterate through all numbers between 1 and 100, and then determine whether each number is odd. If it is an odd number, output or store it in an array. Here is a simple PHP code example:

See all articles