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!

前n个奇数的平方系列取系列中前n个奇数的平方。系列是:1,9,25,49,81,121…该级数也可以写为-12,32,52,72,92,112….这个级数的和有一个数学公式-n(2n+1)(2n-1)/3=n(4n2-1)/3举个例子,Input:N=4Output:sum=解释12+32+52+72=1+9+25+49=84使用公式,和=4(4(4)2-1)/3=4(64-1)/3=4(63)/3=4*21=84这两种方法都是好的,但使用数学公式的方法更好,因为它不使用外观,从而减少了时间复杂

在本节中,我们将看到如何在不使用任何条件语句(如<,<=,!=,>,>=,==)的情况下检查一个数是奇数还是偶数。我们可以通过使用条件语句轻松地检查奇数还是偶数。我们可以将数字除以2,然后检查余数是否为0。如果为0,则是偶数。否则,我们可以将数字与1进行AND运算。如果答案为0,则是偶数,否则为奇数。这里不能使用条件语句。我们将看到两种不同的方法来检查奇数还是偶数。方法1在这里,我们将创建一个字符串数组。索引0位置将保存“偶数”,索引1位置将保存“奇数”。我们可以将数字除

奇数的平均数直到给定的奇数是一个简单的概念。您只需要找到直到该数字的奇数,然后将它们相加并除以该数字。如果要找到直到n的奇数的平均数。然后我们将从1到n找到奇数,然后相加,再除以奇数的数量。例子奇数的平均数直到9是5,即1+3+5+7+9=25=>25/5=5计算奇数的平均数直到n有两种方法,其中n是一个奇数使用循环使用公式程序查找到n为止的奇数的平均值,使用循环为了计算直到n的奇数的平均值,我们将把直到n的所有数字相加,然后除以直到n的奇数的个数。计算平均值的程序奇自然数直到n-示例代码

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

前n个偶数的平方和意味着,我们首先找到平方并将它们全部相加得到总和。有两种方法可以找到前n个偶数的平方和使用循环我们可以使用循环从1到n迭代,每次增加1,找到平方并将其加到总和变量中−例子#include<iostream>usingnamespacestd;intmain(){ intsum=0,n=12; for(inti=1;i<=n;i++) &nb

给定一个包含N个整数的数组和Q个范围查询。对于每个查询,我们需要返回范围内每个数字的最大奇数除数的异或。最大奇数除数是可以整除数字N的最大奇数,例如。例如,6的最大奇数约数是3。Input:nums[]={3,6,7,10},query[]={{0,2},{1,3}}Output:query1:7query2:1Explanation:greatestodddivisorsofnumsarrayare{3,3,7,5}.Forquery1weneedtofindtheXORofindexes0,

PHP是一种流行的服务器端编程语言,广泛用于网站开发和动态网页生成。在PHP中,获取一定范围内的奇数是一个常见的需求。本文将介绍如何使用PHP实现获取100以内的奇数,并提供具体的代码示例。首先,我们可以使用for循环来遍历1到100之间的所有数字,然后判断每个数是否是奇数。如果是奇数,就将其输出或存储到一个数组中。下面是一个简单的PHP代码示例:

前n个自然数的平方和是求最多n项的平方和。本系列求n以内每个数字的和,并将该和添加到sum变量中。前4个自然数的平方和之和为-sum=(12)+(12+22)+(12+22+32)+(12+22+32+42)=1+5+14+30=50有两种方法可以求前n个自然数的平方和。1)使用for循环。在这个方法中,我们将循环遍历从1到N的每个数字并找到平方和,然后将这个平方和添加到sum变量中。此方法需要对n个数字进行迭代,因此对于更大的数字将非常耗时。示例#include<stdio.h>i


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
