前n个自然数的平方和之和

给定一个正整数 N .任务是求前n个自然数的平方和之和。 例如:

null
Input : n = 3Output : 20Sum of square of first natural number = 1Sum of square of first two natural number = 1^2 + 2^2 = 5Sum of square of first three natural number = 1^2 + 2^2 + 3^2 = 14Sum of sum of square of first three natural number = 1 + 5 + 14 = 20Input : n = 2Output : 6

方法1:O(n) 其思想是求第一个i自然数的平方和,其中1<=i<=n,然后将它们相加。 我们可以通过以下公式求出前n个自然数的平方和:n*(n+1)*(2*n+1)/6 以下是该方法的实施情况:

C++

// CPP Program to find the sum of sum of
// squares of first n natural number
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of sum of square of
// first n natural number
int findSum( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += ((i * (i + 1) * (2 * i + 1)) / 6);
return sum;
}
// Driven Program
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}


JAVA

// Java Program to find the sum of
// sum of squares of first n natural
// number
class GFG {
// Function to find sum of sum of
// square of first n natural number
static int findSum( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum += ((i * (i + 1 )
* ( 2 * i + 1 )) / 6 );
return sum;
}
// Driver Program
public static void main(String[] args)
{
int n = 3 ;
System.out.println( findSum(n));
}
}
// This code is contributed by
// Arnab Kundu


Python3

# Python3 Program to find the sum
# of sum of squares of first n
# natural number
# Function to find sum of sum of
# square of first n natural number
def findSum(n):
summ = 0
for i in range ( 1 , n + 1 ):
summ = (summ + ((i * (i + 1 )
* ( 2 * i + 1 )) / 6 ))
return summ
# Driven Program
n = 3
print ( int (findSum(n)))
# This code is contributed by
# Prasad Kshirsagar


C#

// C# Program to find the sum of sum of
// squares of first n natural number
using System;
public class GFG {
// Function to find sum of sum of
// square of first n natural number
static int findSum( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum += ((i * (i + 1) *
(2 * i + 1)) / 6);
return sum;
}
// Driver Program
static public void Main()
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
// This code is contributed by
// Arnab Kundu.


PHP

<?php
// PHP Program to find the sum of
// squares of first n natural number
// Function to find sum of sum of
// square of first n natural number
function findSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum += (( $i * ( $i + 1) *
(2 * $i + 1)) / 6);
return $sum ;
}
// Driver Code
$n = 3;
echo findSum( $n ) ;
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript Program to find the sum of sum of
// squares of first n natural number
// Function to find sum of sum of square
// of first n natural number
function findSum(n)
{
return (n * (n + 1) * (n + 1) * (n + 2)) / 12;
}
// Driven Program
let n = 3;
document.write(findSum(n) + "<br>" );
// This code is contributed by Mayank Tyagi
</script>


输出:

20

时间复杂度:O(n) 辅助空间:O(1) 方法2:O(1) 数学上,我们需要找到,∑((i*(i+1)*(2*i+1)/6),其中1<=i<=n 让我们来解决这个求和问题,

Sum = Σ ((i * (i + 1) * (2*i + 1)/6), where 1 <= i <= n    = (1/6)*(Σ ((i * (i + 1) * (2*i + 1)))    = (1/6)*(Σ ((i2 + i) * (2*i + 1))    = (1/6)*(Σ ((2*i3 + 3*i2 + i))    = (1/6)*(Σ 2*i3 + Σ 3*i2 + Σ i)    = (1/6)*(2*Σ i3 + 3*Σ i2 + Σ i)    = (1/6)*(2*(i*(i + 1)/2)2 + 3*(i * (i + 1) * (2*i + 1))/6 + i * (i + 1)/2)    = (1/6)*(i * (i + 1))((i*(i + 1)/2) + ((2*i + 1)/2) + 1/2)    = (1/12) * (i * (i + 1)) * ((i*(i + 1)) + (2*i + 1) + 1)    = (1/12) * (i * (i + 1)) * ((i2 + 3 * i + 2)    = (1/12) * (i * (i + 1)) * ((i + 1) * (i + 2))    = (1/12) * (i * (i + 1)2 * (i + 2))

以下是该方法的实施情况:

C++

// CPP Program to find the sum of sum of
// squares of first n natural number
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of sum of square
// of first n natural number
int findSum( int n)
{
return (n * (n + 1) * (n + 1) * (n + 2)) / 12;
}
// Driven Program
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}


JAVA

// Java Program to find the sum of sum of
// squares of first n natural number
class GFG {
// Function to find sum of sum of
// square of first n natural number
static int findSum( int n)
{
return (n * (n + 1 ) *
(n + 1 ) * (n + 2 )) / 12 ;
}
// Driver Program
public static void main(String[] args)
{
int n = 3 ;
System.out.println(findSum(n) );
}
}
// This code is contributed by Arnab Kundu


Python3

# Python3 Program to find the sum
# of sum of squares of first n
# natural number
# Function to find sum of sum of
# square of first n natural number
def findSum(n):
return ((n * (n + 1 ) * (n + 1 )
* (n + 2 )) / 12 )
# Driven Program
n = 3
print ( int (findSum(n)))
# This code is contributed by
# Prasad Kshirsagar


C#

// C# Program to find the sum of sum of
// squares of first n natural number
using System;
class GFG {
// Function to find sum of sum of
// square of first n natural number
static int findSum( int n)
{
return (n * (n + 1) * (n + 1)
* (n + 2)) / 12;
}
// Driver Program
static public void Main()
{
int n = 3;
Console.WriteLine(findSum(n) );
}
}
// This code is contributed by Arnab Kundu


PHP

<?php
// PHP Program to find the sum of sum of
// squares of first n natural number
// Function to find sum of sum of square
// of first n natural number
function findSum( $n )
{
return ( $n * ( $n + 1) *
( $n + 1) * ( $n +
2)) / 12;
}
// Driver Code
$n = 3;
echo findSum( $n ) ;
// This code is contributed by nitin mittal
?>


Javascript

<script>
// js Program to find the sum of sum of
// squares of first n natural number
// Function to find sum of sum of square
// of first n natural number
function findSum($n)
{
return (n * (n + 1) *(n + 1) * (n + 2)) / 12;
}
// Driver Code
n = 3;
document.write(findSum(n)) ;
// This code is contributed by sravan kumar
</script>


输出:

20

时间复杂度:O(1) 辅助空间:O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享