📜  求和N且公共差等于1的算术级数

📅  最后修改于: 2021-04-17 14:26:56             🧑  作者: Mango

给定一个整数N ,任务是计算所有公共差等于1且所有元素之和等于N的算术级数

例子:

方法:可以使用AP的以下属性来解决给定的问题

  • 具有第一项为A且共同差为1的N个AP系列总和由公式S =(N / 2)*(2 * A + N -1)给出
  • 求解上面的方程式:

请按照以下步骤解决问题:

  • 初始化一个变量,例如count,以存储在给定条件下可能的AP系列的数量。
  • 遍历2 * N的所有因子,对于每个i因子,检查(2 * N / i)− i + 1是否为偶数。
  • 如果发现为真,则增加计数。
  • 打印计数– 1作为必需答案,因为仅包含N的序列需要丢弃。

下面是上述方法的实现:

C++14
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count all possible
// AP series with common difference
// 1 and sum of elements equal to N
void countAPs(long long int N)
{
    // Stores the count of AP series
    long long int count = 0;
 
    // Traverse through all factors of 2 * N
    for (long long int i = 1;
         i * i <= 2 * N; i++) {
 
        long long int res = 2 * N;
        if (res % i == 0) {
 
            // Check for the given conditions
            long long int op = res / i - i + 1;
 
            if (op % 2 == 0) {
 
                // Increment count
                count++;
            }
            if (i * i != res
                and (i - res / i + 1) % 2 == 0) {
                count++;
            }
        }
    }
 
    // Print count - 1
    cout << count - 1 << "\n";
}
 
// Driver Code
int main()
{
    // Given value of N
    long long int N = 963761198400;
 
    // Function call to count
    // required number of AP series
    countAPs(N);
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
 
  // Function to count all possible
  // AP series with common difference
  // 1 and sum of elements equal to N
  static void countAPs(long N)
  {
 
    // Stores the count of AP series
    long count = 0;
 
    // Traverse through all factors of 2 * N
    for (long i = 1; i * i <= 2 * N; i++) {
 
      long res = 2 * N;
      if (res % i == 0) {
 
        // Check for the given conditions
        long op = res / i - i + 1;
 
        if (op % 2 == 0) {
 
          // Increment count
          count++;
        }
        if (i * i != res
            && (i - res / i + 1) % 2 == 0) {
          count++;
        }
      }
    }
 
    // Print count - 1
    System.out.println(count - 1);
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Given value of N
    long N = 963761198400L;
 
    // Function call to count
    // required number of AP series
    countAPs(N);
  }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program to implement
# the above approach
 
#  Function to count all possible
#  AP series with common difference
#  1 and sum of elements equal to N
def countAPs(N) :
     
    #  Stores the count of AP series
    count = 0
 
    #  Traverse through all factors of 2 * N
    i = 1
    while(i * i <= 2 * N) :
 
        res = 2 * N
        if (res % i == 0) :
 
            #  Check for the given conditions
            op = res / i - i + 1
 
            if (op % 2 == 0) :
 
                #  Increment count
                count += 1
            if (i * i != res
                and (i - res / i + 1) % 2 == 0) :
                count += 1     
        i += 1
   
    #  Prcount - 1
    print(count - 1)
 
 
#  Driver Code
 
#  Given value of N
N = 963761198400
 
#  Function call to count
#  required number of AP series
countAPs(N)
 
# This code is contributed by sanjoy_62.


C#
// C# program for above approach
using System;
public class GFG
{
 
  // Function to count all possible
  // AP series with common difference
  // 1 and sum of elements equal to N
  static void countAPs(long N)
  {
 
    // Stores the count of AP series
    long count = 0;
 
    // Traverse through all factors of 2 * N
    for (long i = 1; i * i <= 2 * N; i++) {
 
      long res = 2 * N;
      if (res % i == 0) {
 
        // Check for the given conditions
        long op = res / i - i + 1;
 
        if (op % 2 == 0) {
 
          // Increment count
          count++;
        }
        if (i * i != res
            && (i - res / i + 1) % 2 == 0) {
          count++;
        }
      }
    }
 
    // Print count - 1
    Console.WriteLine(count - 1);
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    // Given value of N
    long N = 963761198400L;
 
    // Function call to count
    // required number of AP series
    countAPs(N);
  }
}
 
// This code is contributed by sanjoy_62.


输出:
1919

时间复杂度: O(√N)
辅助空间: O(1)