📌  相关文章
📜  生成总和为 N 的不同连续奇数序列的方法计数

📅  最后修改于: 2022-05-13 01:56:07.564000             🧑  作者: Mango

生成总和为 N 的不同连续奇数序列的方法计数

给定一个整数N ,任务是找出由加起来为N不同连续奇数整数组成的序列的总数。

例子

方法:解决问题的思想是基于前K个连续奇数之和的思想:

按照下面提到的步骤来实现上述观察:

  • 遍历所有除数对,使其乘积为N
  • 如果这样一对除数之和是偶数,则将答案的计数加 1。
  • 最后返回最终计数。

下面是上述方法的实现。

C++
// C++ program for above approach:
#include 
using namespace std;
 
// Function to calculate
// Number of sequence of odd integers that
// Contains distinct consecutive odd integers
// That add up to N.
int numberOfSequences(int N)
{
    // Initializing count variable by 0,
    // That stores the number of sequences
    int count = 0;
 
    // Iterating through all divisors of N
    for (int i = 1; i * i <= N; i++) {
        if (N % i == 0) {
 
            // If sum of the two divisors
            // Is even, we increment
            // The count by 1
            int divisor1 = i;
            int divisor2 = N / i;
            int sum = divisor1 + divisor2;
            if (sum % 2 == 0) {
                count++;
            }
        }
    }
 
    // Returning total count
    // After completing the iteration
    return count;
}
 
// Driver Code
int main()
{
    int N = 45;
 
    // Function call
    int number_of_sequences = numberOfSequences(N);
    cout << number_of_sequences;
    return 0;
}


Java
// JAVA program to check whether sum
// Is equal to target value
// After K operations
import java.util.*;
class GFG
{
 
  // Function to calculate
  // Number of sequence of odd integers that
  // Contains distinct consecutive odd integers
  // That add up to N.
  static int numberOfSequences(int N)
  {
    // Initializing count variable by 0,
    // That stores the number of sequences
    int count = 0;
 
    // Iterating through all divisors of N
    for (int i = 1; i * i <= N; i++) {
      if (N % i == 0) {
 
        // If sum of the two divisors
        // Is even, we increment
        // The count by 1
        int divisor1 = i;
        int divisor2 = N / i;
        int sum = divisor1 + divisor2;
        if (sum % 2 == 0) {
          count++;
        }
      }
    }
 
    // Returning total count
    // After completing the iteration
    return count;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 45;
 
    // Function call
    int number_of_sequences = numberOfSequences(N);
    System.out.print(number_of_sequences);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python code for the above approach
import math
 
# Function to calculate
# Number of sequence of odd integers that
# Contains distinct consecutive odd integers
# That add up to N.
def numberOfSequences(N):
 
    # Initializing count variable by 0,
    # That stores the number of sequences
    count = 0;
 
    # Iterating through all divisors of N
    for i in range(1,math.ceil(math.sqrt(N))):
        if (N % i == 0):
           
            # If sum of the two divisors
            # Is even, we increment
            # The count by 1
            divisor1 = i;
            divisor2 = N //i;
            sum = divisor1 + divisor2;
            if (sum % 2 == 0):
                count = count + 1
             
    # Returning total count
    # After completing the iteration
    return count;
 
# Driver Code
N = 45;
 
# Function call
number_of_sequences = numberOfSequences(N);
print(number_of_sequences);
    
# This code is contributed by Potta Lokesh


C#
// C# program for above approach:
using System;
class GFG {
 
  // Function to calculate
  // Number of sequence of odd integers that
  // Contains distinct consecutive odd integers
  // That add up to N.
  static int numberOfSequences(int N)
  {
    // Initializing count variable by 0,
    // That stores the number of sequences
    int count = 0;
 
    // Iterating through all divisors of N
    for (int i = 1; i * i <= N; i++) {
      if (N % i == 0) {
 
        // If sum of the two divisors
        // Is even, we increment
        // The count by 1
        int divisor1 = i;
        int divisor2 = N / i;
        int sum = divisor1 + divisor2;
        if (sum % 2 == 0) {
          count++;
        }
      }
    }
 
    // Returning total count
    // After completing the iteration
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 45;
 
    // Function call
    int number_of_sequences = numberOfSequences(N);
    Console.Write(number_of_sequences);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

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