📜  O(1)复杂度的前n个奇数之和

📅  最后修改于: 2021-05-04 11:33:47             🧑  作者: Mango

给定奇数序列
1、3、5、7、9、11、13、15、17、19、21、23等。
找出前n个奇数的总和
例子:

Input : n = 2
Output : 4
Sum of first two odd numbers is 1 + 3 = 4.

Input : 5
Output : 25
Sum of first 5 odd numbers is 1 + 3 + 5 +
7 + 9 = 25

一个简单的解决方案是遍历所有奇数。

C++
// A naive CPP program to find sum of
// first n odd numbers
#include 
using namespace std;
 
// Returns the sum of first
// n odd numbers
int oddSum(int n)
{
    int sum = 0, curr = 1;
    for (int i = 0; i < n; i++) {
        sum += curr;
        curr += 2;
    }
    return sum;
}
 
// Driver function
int main()
{
    int n = 20;
    cout << " Sum of first " << n
         << " Odd Numbers is: " << oddSum(n);
    return 0;
}


Java
// Java program to find sum of
// first n odd numbers
import java.util.*;
 
class Odd
{  
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        int sum = 0, curr = 1;
        for (int i = 0; i < n; i++) {
            sum += curr;
            curr += 2;
        }
        return sum;
    }
     
    // driver function
    public static void main(String[] args)
    {
        int n = 20;
        System.out.println(" Sum of first "+ n
        +" Odd Numbers is: "+oddSum(n));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to find sum
# of first n odd numbers
 
def oddSum(n) :
    sum = 0
    curr = 1
    i = 0
    while i < n:
        sum = sum + curr
        curr = curr + 2
        i = i + 1
    return sum
 
# Driver Code
n = 20
print (" Sum of first" , n, "Odd Numbers is: ",
                                oddSum(n) )
 
# This code is contributed by rishabh_jain


C#
// C# program to find sum of
// first n odd numbers
using System;
 
class GFG {
     
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        int sum = 0, curr = 1;
        for (int i = 0; i < n; i++) {
            sum += curr;
            curr += 2;
        }
         
        return sum;
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
        Console.WriteLine(" Sum of first " + n
            + " Odd Numbers is: " + oddSum(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Efficient program to find sum of
// first n odd numbers
#include 
using namespace std;
 
// Returns the sum of first
// n odd numbers
int oddSum(int n)
{
    return (n * n);
}
 
// Driver function
int main()
{
    int n = 20;
    cout << " Sum of first " << n
         << " Odd Numbers is: " << oddSum(n);
    return 0;
}


Java
// Java program to find sum of
// first n odd numbers
import java.util.*;
 
class Odd
{  
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        return (n * n);
    }
     
    // driver function
    public static void main(String[] args)
    {
        int n = 20;
        System.out.println(" Sum of first "+ n
        +" Odd Numbers is: "+oddSum(n));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to find sum
# of first n odd numbers
 
def oddSum(n) :
    return (n * n);
 
# Driver Code
n = 20
print (" Sum of first" , n, "Odd Numbers is: ",
                               oddSum(n) )
 
# This code is contributed by rishabh_jain


C#
// C# program to find sum of
// first n odd numbers
using System;
 
class GFG {
     
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        return (n * n);
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
        Console.WriteLine(" Sum of first " + n
            + " Odd Numbers is: " + oddSum(n));
    }
}
 
// This code is contributed by vt_m.


PHP


输出:

Sum of first 20 odd numbers is 400

时间复杂度:O(n)
辅助空间:O(1)一个有效的解决方案是使用直接公式。为了找到前n个奇数的和,我们可以应用奇数定理,它指出前n个奇数的和等于n的平方。

令n = 10,因此前10个奇数之和为
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100
如果我们应用奇数定理:
前10个奇数的总和= n * n = 10 * 10 = 100。
下面是上述方法的实现:

C++

// Efficient program to find sum of
// first n odd numbers
#include 
using namespace std;
 
// Returns the sum of first
// n odd numbers
int oddSum(int n)
{
    return (n * n);
}
 
// Driver function
int main()
{
    int n = 20;
    cout << " Sum of first " << n
         << " Odd Numbers is: " << oddSum(n);
    return 0;
}

Java

// Java program to find sum of
// first n odd numbers
import java.util.*;
 
class Odd
{  
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        return (n * n);
    }
     
    // driver function
    public static void main(String[] args)
    {
        int n = 20;
        System.out.println(" Sum of first "+ n
        +" Odd Numbers is: "+oddSum(n));
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 program to find sum
# of first n odd numbers
 
def oddSum(n) :
    return (n * n);
 
# Driver Code
n = 20
print (" Sum of first" , n, "Odd Numbers is: ",
                               oddSum(n) )
 
# This code is contributed by rishabh_jain

C#

// C# program to find sum of
// first n odd numbers
using System;
 
class GFG {
     
    // Returns the sum of first
    // n odd numbers
    public static int oddSum(int n)
    {
        return (n * n);
    }
 
    // driver function
    public static void Main()
    {
        int n = 20;
        Console.WriteLine(" Sum of first " + n
            + " Odd Numbers is: " + oddSum(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出:

Sum of first 20 odd numbers is 400

时间复杂度:O(1)
辅助空间:O(1)
它是如何工作的?
我们可以使用数学归纳法证明这一点。我们知道n = 1和n = 2是正确的,因为总和分别为1和4(1 + 3)。

Let it be true for n = k-1.

Sum of first k odd numbers = 
  Sum of first k-1 odd numbers + k'th odd number
= (k-1)*(k-1) + (2k - 1)
= k*k