📌  相关文章
📜  系列的总和(1 * 2)+(2 * 3)+(3 * 4)+……最多n个项

📅  最后修改于: 2021-04-27 05:02:40             🧑  作者: Mango

给定值n,任务是找到级数(1 * 2)+(2 * 3)+(3 * 4)+……+ n个项的和
例子:

Input: n = 2
Output: 8
Explanation:
(1*2) + (2*3)
= 2 + 6
= 8

Input: n = 3
Output: 20
Explanation:
(1*2) + (2*3) + (2*4)
= 2 + 6 + 12
= 20

简单的解决方案递归地添加元素。
下面是实现

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return sum
int sum(int n)
{
    if (n == 1) {
        return 2;
    }
    else {
        return (n * (n + 1) + sum(n - 1));
    }
}
 
// Driver code
int main()
{
 
    int n = 2;
    cout << sum(n);
}


Java
// Java implementation of the approach
 
class Solution {
 
    // Function to return a the required result
    static int sum(int n)
    {
        if (n == 1) {
            return 2;
        }
        else {
            return (n * (n + 1) + sum(n - 1));
        }
    }
    // Driver code
    public static void main(String args[])
    {
        int n = 2;
        System.out.println(sum(n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return sum
def sum(n):
 
    if (n == 1):
        return 2;
    else:
        return (n * (n + 1) + sum(n - 1));
 
# Driver code
 
n = 2;
print(sum(n));
 
# This code is contributed by mits


C#
// Csharp implementation of the approach
 
using System;
 
class Solution {
 
    // Function to return a the required result
    static int sum(int n)
    {
        if (n == 1) {
            return 2;
        }
        else {
            return (n * (n + 1) + sum(n - 1));
        }
    }
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WrieLine(sum(n));
    }
}


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return sum
int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
 
// Driver code
int main()
{
    int n = 2;
    cout << sum(n);
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return sum
static int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2;
    System.out.println(sum(n));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach.
 
# Function to return sum
def Sum(n):
 
    return n * (n + 1) * (n + 2) // 3
 
# Driver code
if __name__ == "__main__":
 
    n = 2;
    print(Sum(n))
 
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the approach
using System;
     
class GFG
{
      
// Function to return sum
static int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 2;
    Console.WriteLine(sum(n));
}
}
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
8

时间复杂度: O(n)
高效的解决方案我们可以使用直接公式解决此问题。
总和可以写成如下
∑(n *(n + 1))
∑(n * n + n)
= ∑(n * n)+ ∑(n)
我们可以将公式应用于自然数之和平方和自然数之和。
= n(n + 1)(2n + 1)/ 6 + n *(n + 1)/ 2
= n *(n + 1)*(n + 2)/ 3

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return sum
int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
 
// Driver code
int main()
{
    int n = 2;
    cout << sum(n);
}

Java

// Java implementation of the approach
class GFG
{
     
// Function to return sum
static int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 2;
    System.out.println(sum(n));
}
}
 
// This code is contributed by Code_Mech

Python3

# Python3 implementation of the approach.
 
# Function to return sum
def Sum(n):
 
    return n * (n + 1) * (n + 2) // 3
 
# Driver code
if __name__ == "__main__":
 
    n = 2;
    print(Sum(n))
 
# This code is contributed
# by Rituraj Jain

C#

// C# implementation of the approach
using System;
     
class GFG
{
      
// Function to return sum
static int sum(int n)
{
    return n * (n + 1) * (n + 2) / 3;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 2;
    Console.WriteLine(sum(n));
}
}
// This code contributed by Rajput-Ji

的PHP


Java脚本


输出:
8

时间复杂度: O(1)