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 =
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; }
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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 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

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

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

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 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:
