给定数字n,请找到前n个奇数自然数的平方和。
例子 :
Input : 3
Output : 35
12 + 32 + 52 = 35
Input : 8
Output : 680
12 + 32 + 52 + 72 + 92 + 112 + 132 + 152
一个简单的解决方案是遍历n个奇数并找到平方和。
下面是该方法的实现。
C++
// Simple C++ method to find sum of square of
// first n odd numbers.
#include
using namespace std;
int squareSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += (2*i - 1) * (2*i - 1);
return sum;
}
int main()
{
cout << squareSum(8);
return 0;
}
Java
// Simple Java method to
// find sum of square of
// first n odd numbers.
import java.io.*;
class GFG {
static int squareSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += (2*i - 1) * (2*i - 1);
return sum;
}
//Driver Code
public static void main(String args[])
{
System.out.println(squareSum(8));
}
}
// This code is contributed by
// Nikita tiwari.
Python3
# Simple Python method
# to find sum of square
# of first n odd numbers.
def squareSum(n):
sm = 0
for i in range(1, n + 1):
sm += (2 * i - 1) * (2 * i - 1)
return sm
# Driver Code
n=8
print(squareSum(n))
# This code is contributed by Ansu Kumari
C#
// Simple C# method to find
// sum of square of first
// n odd numbers.
using System;
class GFG {
static int squareSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += (2*i - 1) * (2*i - 1);
return sum;
}
// Driver Code
public static void Main()
{
Console.Write(squareSum(8));
}
}
// This code is contributed by
// vt_m.
PHP
Javascript
C++
// Efficient C++ method to find sum of
// square of first n odd numbers.
#include
using namespace std;
int squareSum(int n)
{
return n*(4*n*n - 1)/3;
}
int main()
{
cout << squareSum(8);
return 0;
}
Java
// Efficient Java method
// to find sum of
// square of first n odd numbers.
import java.io.*;
class GFG {
static int squareSum(int n)
{
return n*(4*n*n - 1)/3;
}
public static void main(String args[])
{
System.out.println(squareSum(8));
}
}
// This code is contributed by
// Nikita tiwari.
Python3
# Python3 code to find sum
# of square of first n odd numbers
def squareSum( n ):
return int(n * ( 4 * n * n - 1) / 3)
# driver code
ans = squareSum(8)
print (ans)
# This code is contributed by Saloni Gupta
C#
// Efficient C# method to
// find sum of square of
// first n odd numbers.
using System;
class GFG {
static int squareSum(int n)
{
return n * (4 * n * n - 1)/3;
}
// driver code
public static void Main()
{
Console.Write(squareSum(8));
}
}
// This code is contributed by
// Vt_m.
PHP
Javascript
输出 :
680
一个有效的解决方案是应用以下公式。
sum = n * (4n2 - 1) / 3
How does it work?
Please refer sum of squares of even and odd
numbers for proof.
C++
// Efficient C++ method to find sum of
// square of first n odd numbers.
#include
using namespace std;
int squareSum(int n)
{
return n*(4*n*n - 1)/3;
}
int main()
{
cout << squareSum(8);
return 0;
}
Java
// Efficient Java method
// to find sum of
// square of first n odd numbers.
import java.io.*;
class GFG {
static int squareSum(int n)
{
return n*(4*n*n - 1)/3;
}
public static void main(String args[])
{
System.out.println(squareSum(8));
}
}
// This code is contributed by
// Nikita tiwari.
Python3
# Python3 code to find sum
# of square of first n odd numbers
def squareSum( n ):
return int(n * ( 4 * n * n - 1) / 3)
# driver code
ans = squareSum(8)
print (ans)
# This code is contributed by Saloni Gupta
C#
// Efficient C# method to
// find sum of square of
// first n odd numbers.
using System;
class GFG {
static int squareSum(int n)
{
return n * (4 * n * n - 1)/3;
}
// driver code
public static void Main()
{
Console.Write(squareSum(8));
}
}
// This code is contributed by
// Vt_m.
的PHP
Java脚本
输出 :
680