📜  前n个自然数的平方和

📅  最后修改于: 2021-05-06 09:15:49             🧑  作者: Mango

给定n,找到前n个自然数的平方和。
例子 :

Input : n = 2
Output : 5
Explanation: 1^2+2^2 = 5

Input : n = 8
Output : 204
Explanation : 
1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 = 204 

天真的方法:
天真的方法是从1到n循环并求和所有平方。

C++
// CPP program to calculate
// 1^2+2^2+3^2+...
#include 
using namespace std;
 
// Function to calculate sum
int summation(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += (i * i);
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 2;
    cout << summation(n);
    return 0;
}


Java
// Java program to calculate
// 1^2+2^2+3^2+...
import java.util.*;
import java.lang.*;
 
class GFG
{
    // Function to calculate sum
    public static int summation(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += (i * i);
 
        return sum;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 2;
        System.out.println(summation(n));
    }
}
 
// This code is contributed
// by Sachin Bisht


Python3
# Python3 program to
# calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2+..
 
# Function to calculate series
def summation(n):
    return sum([i**2 for i in
               range(1, n + 1)])
 
# Driver Code
if __name__ == "__main__":
    n = 2
    print(summation(n))


C#
// C# program to calculate
// 1^2+2^2+3^2+...
using System;
class GFG
{
 
    // Function to calculate sum
    public static int summation(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum += (i * i);
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 2;
 
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// C++ program to get the sum
// of the following series
#include 
using namespace std;
 
// Function calculating
// the series
int summation(int n)
{
    return (n * (n + 1) *
        (2 * n + 1)) / 6;
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << summation(n) << endl;
    return 0;
}
 
// This code is contributed by shubhamsingh10


C
// C program to get the sum
// of the following series
#include 
 
// Function calculating
// the series
int summation(int n)
{
    return (n * (n + 1) *
           (2 * n + 1)) / 6;
}
 
// Driver Code
int main()
{
    int n = 10;
    printf("%d", summation(n));
    return 0;
}


Java
// Java program to get
// the sum of the series
import java.util.*;
import java.lang.*;
 
class GFG
{
    // Function calculating
    // the series
    public static int summation(int n)
    {
        return (n * (n + 1) *
               (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(summation(n));
    }
}
 
// This code is contributed
// by Sachin Bisht


Python3
# Python code to find sum of
# squares of first n natural numbers.
def summation(n):
    return (n * (n + 1) *
           (2 * n + 1)) / 6
     
# Driver Code
if __name__ == '__main__':
    n = 10
    print(summation(n))


C#
// C# program to get the sum
// of the series
using System;
 
class GFG
{
 
    // Function calculating
    // the series
    public static int summation(int n)
    {
        return (n * (n + 1) *
               (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
 
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript

   


输出 :

5

高效的方法:
存在一个用于找到前n个数字的平方和的公式。
1 + 2 +……….. + n = n(n + 1)/ 2
1 2 + 2 2 +………+ n 2 = n(n + 1)(2n + 1)/ 6

n * (n + 1) * (2*n + 1) / 6

Example : n = 3
        = 3 * (3 + 1) * (2*3 + 1) / 6
        = (3 * 4 * 7) / 6
        = 84 / 6
        = 14

这是如何运作的?

We can prove this formula using induction.
We can easily see that the formula is true for
n = 1 and n = 2 as sums are 1 and 5 respectively.

Let it be true for n = k-1. So sum of k-1 numbers
is (k - 1) * k * (2 * k - 1)) / 6

In the following steps, we show that it is true 
for k assuming that it is true for k-1.

Sum of k numbers = Sum of k-1 numbers + k2
           = (k - 1) * k * (2 * k - 1) / 6 + k2
           = ((k2 - k) * (2*k - 1) + 6k2)/6
           = (2k3 - 2k2 - k2 + k + 6k2)/6
           = (2k3 + 3k2 + k)/6
           = k * (k + 1) * (2*k + 1) / 6

C++

// C++ program to get the sum
// of the following series
#include 
using namespace std;
 
// Function calculating
// the series
int summation(int n)
{
    return (n * (n + 1) *
        (2 * n + 1)) / 6;
}
 
// Driver Code
int main()
{
    int n = 10;
    cout << summation(n) << endl;
    return 0;
}
 
// This code is contributed by shubhamsingh10

C

// C program to get the sum
// of the following series
#include 
 
// Function calculating
// the series
int summation(int n)
{
    return (n * (n + 1) *
           (2 * n + 1)) / 6;
}
 
// Driver Code
int main()
{
    int n = 10;
    printf("%d", summation(n));
    return 0;
}

Java

// Java program to get
// the sum of the series
import java.util.*;
import java.lang.*;
 
class GFG
{
    // Function calculating
    // the series
    public static int summation(int n)
    {
        return (n * (n + 1) *
               (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(summation(n));
    }
}
 
// This code is contributed
// by Sachin Bisht

Python3

# Python code to find sum of
# squares of first n natural numbers.
def summation(n):
    return (n * (n + 1) *
           (2 * n + 1)) / 6
     
# Driver Code
if __name__ == '__main__':
    n = 10
    print(summation(n))

C#

// C# program to get the sum
// of the series
using System;
 
class GFG
{
 
    // Function calculating
    // the series
    public static int summation(int n)
    {
        return (n * (n + 1) *
               (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
 
        Console.WriteLine(summation(n));
    }
}
 
// This code is contributed by vt_m

的PHP


Java脚本

   

输出 :

385