📜  一系列第一个奇数然后偶数自然数的范围之和

📅  最后修改于: 2021-05-06 21:05:04             🧑  作者: Mango

该序列首先由从1到n的所有奇数组成,然后由从2到n的其余偶数组成。假设n为1000,则序列变为1 3 5 7….999 2 4 6….1000
给定一个范围(L,R),我们需要找到给定范围内该序列的数字之和。
注意:此处的范围为(L,R)L和R包含在范围内
例子:

Input  : n = 10
         Range 1 6
Output : 27
Explanation:
Sequence is 1 3 5 7 9 2 4 6 8 10
Sum in range (2, 6) 
= 1 + 3 + 5 + 7 + 9 + 2 
= 27

Input  : n = 5
         Range 1 2
Output : 4
Explanation:
sequence is 1 3 5 2 4
sum = 1 + 3 = 4

这个想法是首先找到左(不包括左)之前的数字总和,然后找到右(包括右)之前的数字总和。我们得到的结果为第二总和减去第一总和。
如何求和到极限?
我们首先计算有多少个奇数,然后使用公式对奇数自然数和和偶数自然数进行求和。
如何找到奇数计数?

  • 如果n为奇数,则奇数为((n / 2)+ 1)
  • 如果n为偶数,则奇数为(n / 2)

通过简单的观察,我们得到奇数个数为ceil(n / 2)。因此,偶数为n – ceil(n / 2)。

  • 前N个奇数之和为(N ^ 2)
  • 前N个偶数之和为(N ^ 2)+ N

对于给定的数字x,我们如何找到从1到x的序列之和?
假设x小于奇数个数。

  • 然后我们简单地返回(x * x)

如果x更大,则为奇数

  • var = x-odd;
  • 这意味着我们需要第一个var偶数
  • 我们返回(odd * odd)+(var * var)+ var;
C++
// CPP program to find sum in the given range in
// the sequence 1 3 5 7.....N 2 4 6...N-1
#include 
using namespace std;
 
// For our convenience
#define ll long long
 
// Function that returns sum
// in the range 1 to x in the
// sequence 1 3 5 7.....N 2 4 6...N-1
ll sumTillX(ll x, ll n)
{
    // number of odd numbers
    ll odd = ceil(n / 2.0);
 
    if (x <= odd)
       return x * x;
 
    // number of extra even
    // numbers required
    ll even = x - odd;
 
    return ((odd * odd) + (even * even) + even);
}
 
int rangeSum(int N, int L, int R)
{
   return sumTillX(R, N) - sumTillX(L-1, N);
}
 
// Driver code
int main()
{
    ll N = 10, L = 1, R = 6;   
    cout << rangeSum(N, L, R);
    return 0;
}


Java
// Java program to find
// sum in the given
// range in the sequence
// 1 3 5 7.....N
// 2 4 6...N-1
 
class GFG {
     
    // Function that returns sum
    // in the range 1 to x in the
    // sequence 1 3 5 7.....N 2 4 6...N-1
    static double sumTillX(double x,
                           double n)
    {
         
        // number of odd numbers
        double odd = Math.ceil(n / 2.0);
     
        if (x <= odd)
            return x * x;
     
        // number of extra even
        // numbers required
        double even = x - odd;
     
        return ((odd * odd) + (even *
                       even) + even);
    }
     
    static double rangeSum(double N,
                           double L,
                           double R)
    {
        return sumTillX(R, N) -
               sumTillX(L-1, N);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        long N = 10, L = 1, R = 6;
        int n = 101;
        System.out.println((int)rangeSum(N, L, R));
         
    }
}
 
// This code is contributed by Sam007


Python 3
# Python 3 program to find sum in the
# given range in the sequence 1 3 5 7
# .....N 2 4 6...N-1
import math
 
# For our convenience
#define ll long long
 
# Function that returns sum in the
# range 1 to x in the sequence
# 1 3 5 7.....N 2 4 6...N-1
def sumTillX(x, n):
 
    # number of odd numbers
    odd = math.ceil(n / 2.0)
 
    if (x <= odd):
        return x * x;
 
    # number of extra even
    # numbers required
    even = x - odd;
 
    return ((odd * odd) +
            (even * even) + even);
 
 
def rangeSum(N, L, R):
 
    return (sumTillX(R, N) - 
                sumTillX(L-1, N));
 
# Driver code
N = 10
L = 1
R = 6
print(rangeSum(N, L, R))
 
# This code is contributed by
# Smitha


C#
// C# program to find sum in the given
// range in the sequence 1 3 5 7.....N
// 2 4 6...N-1
using System;
 
public class GFG {
         
    // Function that returns sum
    // in the range 1 to x in the
    // sequence 1 3 5 7.....N 2 4 6...N-1
    static double sumTillX(double x, double n)
    {
         
        // number of odd numbers
        double odd = Math.Ceiling(n / 2.0);
     
        if (x <= odd)
            return x * x;
     
        // number of extra even
        // numbers required
        double even = x - odd;
     
        return ((odd * odd) + (even * even)
                                    + even);
    }
     
    static double rangeSum(double N, double L,
                                       double R)
    {
        return sumTillX(R, N) - sumTillX(L-1, N);
    }
     
    // Driver code
    public static void Main()
    {
        long N = 10, L = 1, R = 6;
        Console.Write(rangeSum(N, L, R));
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出:
27