📜  前n个偶数之和

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

给定数字n 。问题是要找到前n个偶数之和。
例子:

Input : n = 4
Output : 20
Sum of first 4 even numbers
= (2 + 4 + 6 + 8) = 20 

Input : n = 20
Output : 420

天真的方法:迭代前n个偶数并相加。

C++
// C++ implementation to find sum of
// first n even numbers
#include 
 
using namespace std;
 
// function to find sum of
// first n even numbers
int evenSum(int n)
{
    int curr = 2, sum = 0;
 
    // sum of first n even numbers
    for (int i = 1; i <= n; i++) {
        sum += curr;
 
        // next even number
        curr += 2;
    }
 
    // required sum
    return sum;
}
 
// Driver program to test above
int main()
{
    int n = 20;
    cout << "Sum of first " << n
         << " Even numbers is: " << evenSum(n);
    return 0;
}


Java
// Java implementation to find sum
// of first n even numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
        int curr = 2, sum = 0;
 
        // sum of first n even numbers
        for (int i = 1; i <= n; i++) {
            sum += curr;
 
            // next even number
            curr += 2;
        }
 
        // required sum
        return sum;    
    }
     
    // driver function
    public static void main(String argc[])
    {
        int n = 20;
        System.out.println("Sum of first " + n +
                          " Even numbers is: " +
                          evenSum(n));
    }
 
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 implementation to find sum of
# first n even numbers
     
# function to find sum of
# first n even numbers
def evensum(n):
    curr = 2
    sum = 0
    i = 1
     
    # sum of first n even numbers
    while i <= n:
        sum += curr
         
        # next even number
        curr += 2
        i = i + 1
    return sum
 
# Driver Code
n = 20
print("sum of first ", n, "even number is: ",
      evensum(n))
 
# This article is contributed by rishabh_jain


C#
// C# implementation to find sum
// of first n even numbers
using System;
 
public class GfG {
 
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
        int curr = 2, sum = 0;
 
        // sum of first n even numbers
        for (int i = 1; i <= n; i++) {
            sum += curr;
 
            // next even number
            curr += 2;
        }
 
        // required sum
        return sum;
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
         
        Console.WriteLine("Sum of first " + n
         + " Even numbers is: " + evenSum(n));
    }
}
 
// This code is contributed by vt-m.


PHP


Javascript


C++
// C++ implementation to find sum of
// first n even numbers
#include 
using namespace std;
 
// function to find sum of
// first n even numbers
int evenSum(int n)
{
    // required sum
    return (n * (n + 1));
}
 
// Driver program to test above
int main()
{
    int n = 20;
    cout << "Sum of first " << n
         << " Even numbers is: " << evenSum(n);
    return 0;
}


Java
// Java implementation to find sum
// of first n even numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
        // required sum
        return (n * (n + 1));
    }
     
    // driver function
    public static void main(String argc[])
    {
        int n = 20;
        System.out.println("Sum of first " + n +
                          " Even numbers is: " +
                            evenSum(n));
    }
}
 
 
// This code is contributed by Prerna Saini


Python3
# Python3 implementation to find
# sum of first n even numbers
     
# function to find sum of
# first n even numbers
def evensum(n):
    return n * (n + 1)
 
# Driver Code
n = 20
print("sum of first", n, "even number is: ",
       evensum(n))
 
# This article is contributed by rishabh_jain


C#
// C# implementation to find sum
// of first n even numbers'
using System;
 
public class GfG {
 
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
         
        // required sum
        return (n * (n + 1));
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
         
        Console.WriteLine("Sum of first " + n
        + " Even numbers is: " + evenSum(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出:

Sum of first 20 Even numbers is: 420

时间复杂度: O(n)。
高效方法:通过应用下面给出的公式。

Sum of first n even numbers = n * (n + 1).

证明:

Sum of first n terms of an A.P.(Arithmetic Progression)
= (n/2) * [2*a + (n-1)*d].....(i)
where, a is the first term of the series and d is
the difference between the adjacent terms of the series.

Here, a = 2, d = 2, applying these values to eq.(i), we get
Sum = (n/2) * [2*2 + (n-1)*2]
    = (n/2) * [4 + 2*n - 2]
    = (n/2) * (2*n + 2)
    = n * (n + 1)

C++

// C++ implementation to find sum of
// first n even numbers
#include 
using namespace std;
 
// function to find sum of
// first n even numbers
int evenSum(int n)
{
    // required sum
    return (n * (n + 1));
}
 
// Driver program to test above
int main()
{
    int n = 20;
    cout << "Sum of first " << n
         << " Even numbers is: " << evenSum(n);
    return 0;
}

Java

// Java implementation to find sum
// of first n even numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
        // required sum
        return (n * (n + 1));
    }
     
    // driver function
    public static void main(String argc[])
    {
        int n = 20;
        System.out.println("Sum of first " + n +
                          " Even numbers is: " +
                            evenSum(n));
    }
}
 
 
// This code is contributed by Prerna Saini

Python3

# Python3 implementation to find
# sum of first n even numbers
     
# function to find sum of
# first n even numbers
def evensum(n):
    return n * (n + 1)
 
# Driver Code
n = 20
print("sum of first", n, "even number is: ",
       evensum(n))
 
# This article is contributed by rishabh_jain

C#

// C# implementation to find sum
// of first n even numbers'
using System;
 
public class GfG {
 
    // function to find sum of
    // first n even numbers
    static int evenSum(int n)
    {
         
        // required sum
        return (n * (n + 1));
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
         
        Console.WriteLine("Sum of first " + n
        + " Even numbers is: " + evenSum(n));
    }
}
 
// This code is contributed by vt_m

的PHP


Java脚本


输出:

Sum of first 20 Even numbers is: 420

时间复杂度: O(1)。