📜  求和S且公称差等于D的算术级数

📅  最后修改于: 2021-04-17 18:44:52             🧑  作者: Mango

给定两个整数SD ,任务是计算和S和公共差D可能的算术级数。

例子:

方法:可以根据以下观察结果解决给定问题:

  • AP系列的总和由下式给出:
  • 重新排列以上表达式后:
  • 从以上两个表达式:
    • 这个想法是考虑所有2 * S的因子,并检查是否存在任何因子F ,以使F(2 * a +(F – 1)* d)的乘积等于2 *S 。如果发现是真的,则对具有给定总和S的可能AP之一计算该因数。
    • 如果存在任何因子F ,使得(D * F –(2 * S / F)+ D)可被2整除,则对具有给定总和S的可能AP之一计算该因子。

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

  • 初始化一个变量,例如answer ,以存储具有和S和共同差DAP的计数。
  • 迭代[1,√2* S]范围并检查2 * S是否可被i整除,然后执行以下步骤:
    • 如果(((2 * S / i)+ 1 – i * D)的值可被2整除,则将answer增加1
    • 如果(i * D – S / i + 1)的值可被2整除,则将答案增加1
  • 完成上述步骤后,将answer的值打印为AP的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of APs
// with sum S and common difference D
int countAPs(int S, int D)
{
    // Multiply S by 2
    S = S * 2;
 
    // Stores the count of APs
    int answer = 0;
 
    // Iterate over the factors of 2*S
    for (int i = 1; i <= sqrt(S); i++) {
 
        // Check if i is the factor
        // or not
        if (S % i == 0) {
 
            // Conditions to check if AP
            // can be formed using factor F
            if (((S / i) - D * i + D) % 2 == 0)
                answer++;
 
            if ((D * i - (S / i) + D) % 2 == 0)
                answer++;
        }
    }
 
    // Return the total count of APs
    return answer;
}
 
// Driver Code
int main()
{
    int S = 12, D = 1;
    cout << countAPs(S, D);
 
    return 0;
}


Java
// Java program for above approach
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
   
// Function to count the number of APs
// with sum S and common difference D
static int countAPs(int S, int D)
{
   
    // Multiply S by 2
    S = S * 2;
 
    // Stores the count of APs
    int answer = 0;
 
    // Iterate over the factors of 2*S
    for (int i = 1; i <= Math.sqrt(S); i++) {
 
        // Check if i is the factor
        // or not
        if (S % i == 0) {
 
            // Conditions to check if AP
            // can be formed using factor F
            if (((S / i) - D * i + D) % 2 == 0)
                answer++;
 
            if ((D * i - (S / i) + D) % 2 == 0)
                answer++;
        }
    }
 
    // Return the total count of APs
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    int S = 12, D = 1;
    System.out.println(countAPs(S, D));
}
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python3 program for the above approach
 
# Function to count the number of APs
# with sum S and common difference D
def countAPs(S, D):
   
    # Multiply S by 2
    S = S * 2
 
    # Stores the count of APs
    answer = 0
 
    # Iterate over the factors of 2*S
    for i in range(1, S):
 
        if i * i > S:
            break
             
        # Check if i is the factor
        # or not
        if (S % i == 0):
 
            # Conditions to check if AP
            # can be formed using factor F
            if (((S // i) - D * i + D) % 2 == 0):
                answer += 1
 
            if ((D * i - (S // i) + D) % 2 == 0):
                answer += 1
 
    # Return the total count of APs
    return answer
 
# Driver Code
if __name__ == '__main__':
    S, D = 12, 1
    print(countAPs(S, D));
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count the number of APs
  // with sum S and common difference D
  static int countAPs(int S, int D)
  {
 
    // Multiply S by 2
    S = S * 2;
 
    // Stores the count of APs
    int answer = 0;
 
    // Iterate over the factors of 2*S
    for (int i = 1; i <= Math.Sqrt(S); i++) {
 
      // Check if i is the factor
      // or not
      if (S % i == 0) {
 
        // Conditions to check if AP
        // can be formed using factor F
        if (((S / i) - D * i + D) % 2 == 0)
          answer++;
 
        if ((D * i - (S / i) + D) % 2 == 0)
          answer++;
      }
    }
 
    // Return the total count of APs
    return answer;
  }
 
 
  // Driver code
  static void Main()
  {
    int S = 12, D = 1;
    Console.Write(countAPs(S, D));
  }
}
 
// This code is contributed by sanjoy_62.


输出:
4

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