📌  相关文章
📜  程序查找系列1 + 2 + 2 + 3 + 3 + 3 +的总和。 。 。 + n

📅  最后修改于: 2021-04-23 15:33:49             🧑  作者: Mango

给定一个正整数n,任务是找到级数1 + 2 + 2 + 3 + 3 + 3 +的和。 。 。 + n。
例子:

Input : n = 5
Output : 55
   = 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 + 
     4 + 5 + 5 + 5 + 5 + 5.
   = 55

Input : n = 10
Output : 385

加法:加法将所有元素一一求和。
以下是此方法的实现。

C++
// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include 
using namespace std;
 
// Function that find
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= i; j++)
            sum = sum + i;
    return sum;
}
 
// Driver function
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java Program to
// find sum of
// series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG{
 
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
         
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                sum = sum + i;
         
        return sum;
    }
     
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by Gitanjali


Python3
# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
 
# Driver method
n = 10
 
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali


C#
// C# Program to find sum of
// series 1 + 2 + 2 + 3 + . . . + n
using System;
 
public class GfG {
 
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
 
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
                sum = sum + i;
 
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.Write(sumOfSeries(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include 
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + i * i;
    return sum;
}
 
// Driver function.
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG{
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by Gitanjali


Python3
# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
 
# Driver method
n = 10
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali.


C#
// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
class GfG {
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
         
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// C++ Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include 
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    return (n * (n + 1) * (2 * n + 1)) / 6;
}
 
// Driver function
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG
{
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
     
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by 'Gitanjali'.


Python3
# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    return ((n * (n + 1) * (2 * n + 1)) / 6)
 
# Driver method
n = 10
 
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali


C#
// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
public class GfG {
     
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by 'vt_m'.


PHP


Javascript


输出:

385

时间复杂度:O(n 2 )
乘法法:在乘法法中,每个元素自身相乘然后相加。

Input n = 10
   sum = 1 + 2 + 2 + 3 + 3 + 3 + 4 + . . . + 10
       = 1 + 2 * 2 + 3 * 3 + 4 * 4 + . . . + 10 * 10
       = 1 + 4 + 9 + 16 + . . . + 100
       = 385

C++

// Program to find
// sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include 
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum = sum + i * i;
    return sum;
}
 
// Driver function.
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG{
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by Gitanjali

Python3

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    sum = 0
    for i in range(1, n+1):
        sum = sum + i * i
    return sum
 
# Driver method
n = 10
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali.

C#

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
class GfG {
 
    // Function that find sum of series.
    static int sumOfSeries(int n)
    {
        int sum = 0;
        for (int i = 1; i <= n; i++)
            sum = sum + i * i;
        return sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
         
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出:

385

时间复杂度:O(n)
使用公式:我们还使用公式来查找级数之和。

Input n = 10;
   Sum of series = (n * (n + 1) * (2 * n + 1)) / 6
    put n = 10 in the above formula
    sum = (10 * (10 + 1) * (2 * 10 + 1)) / 6
        = (10 * 11 * 21) / 6
        = 385

C++

// C++ Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
#include 
using namespace std;
 
// Function to find
// sum of series.
int sumOfSeries(int n)
{
    return (n * (n + 1) * (2 * n + 1)) / 6;
}
 
// Driver function
int main()
{
    int n = 10;
 
    // Function call
    cout << sumOfSeries(n);
    return 0;
}

Java

// Java Program to
// find sum of series
// 1 + 2 + 2 + 3 +
// . . . + n
public class GfG
{
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
     
    // Driver Code
    public static void main(String s[])
    {
        int n = 10;
        System.out.println(sumOfSeries(n));
         
    }
}
 
// This code is contributed by 'Gitanjali'.

Python3

# Python3 Program to
# find sum of series
# 1 + 2 + 2 + 3 +
# . . . + n
import math
 
# Function that find
# sum of series.
def sumOfSeries( n):
    return ((n * (n + 1) * (2 * n + 1)) / 6)
 
# Driver method
n = 10
 
# Function call
print (sumOfSeries(n))
 
# This code is contributed by Gitanjali

C#

// C# Program to find sum of series
// 1 + 2 + 2 + 3 + . . . + n
using System;
 
public class GfG {
     
    // Function that find
    // sum of series.
    static int sumOfSeries(int n)
    {
        return (n * (n + 1) * (2 * n + 1)) / 6;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 10;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by 'vt_m'.

的PHP


Java脚本


输出 :

385

时间复杂度:O(1)
有关以上公式的详细信息和更多优化,请参考自然数的平方和。