📌  相关文章
📜  从前 N 个自然数中计算三元组 (a, b, c) 的数量,使得 a * b + c = N

📅  最后修改于: 2021-10-25 11:17:03             🧑  作者: Mango

给定一个整数N ,任务是计算前N 个自然数中的三元组 ( a, b, c ),使得a * b + c = N

例子:

方法:该问题可以基于以下观察来解决:

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

  • 初始化一个变量,比如cntTriplets ,以存储满足给定条件的前N 个自然数的三元组的计数。
  • 使用变量i迭代范围[1, N – 1]并检查N % i == 0是否。如果发现为真,则更新cntTriplets += (N / i) – 1
  • 否则,更新cntTriplet += (N / i)。
  • 最后,打印cntTriplets的值。

下面是上述方法的实现。

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the count of
// triplets (a, b, c) with a * b + c = N
int findCntTriplet(int N)
{
    // Stores count of triplets of 1st
    // N natural numbers which are of
    // the form a * b + c = N
    int cntTriplet = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i < N; i++) {
 
        // If N is divisible by i
        if (N % i != 0) {
 
            // Update cntTriplet
            cntTriplet += N / i;
        }
        else {
 
            // Update cntTriplet
            cntTriplet += (N / i) - 1;
        }
    }
    return cntTriplet;
}
 
// Driver Code
int main()
{
    int N = 3;
    cout << findCntTriplet(N);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to find the count of
  // triplets (a, b, c) with a * b + c = N
  static int findCntTriplet(int N)
  {
 
    // Stores count of triplets of 1st
    // N natural numbers which are of
    // the form a * b + c = N
    int cntTriplet = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i < N; i++)
    {
 
      // If N is divisible by i
      if (N % i != 0)
      {
 
        // Update cntTriplet
        cntTriplet += N / i;
      }
      else
      {
 
        // Update cntTriplet
        cntTriplet += (N / i) - 1;
      }
    }
    return cntTriplet;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 3;
    System.out.println(findCntTriplet(N));
  }
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program to implement
# the above approach
 
# Function to find the count of
# triplets (a, b, c) with a * b + c = N
def findCntTriplet(N):
   
    # Stores count of triplets of 1st
    # N natural numbers which are of
    # the form a * b + c = N
    cntTriplet = 0;
 
    # Iterate over the range [1, N]
    for i in range(1, N):
 
        # If N is divisible by i
        if (N % i != 0):
 
            # Update cntTriplet
            cntTriplet += N // i;
        else:
 
            # Update cntTriplet
            cntTriplet += (N // i) - 1;
 
    return cntTriplet;
 
# Driver code
if __name__ == '__main__':
    N = 3;
    print(findCntTriplet(N));
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the count of
  // triplets (a, b, c) with a * b + c = N
  static int findCntTriplet(int N)
  {
 
    // Stores count of triplets of 1st
    // N natural numbers which are of
    // the form a * b + c = N
    int cntTriplet = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i < N; i++)
    {
 
      // If N is divisible by i
      if (N % i != 0)
      {
 
        // Update cntTriplet
        cntTriplet += N / i;
      }
      else
      {
 
        // Update cntTriplet
        cntTriplet += (N / i) - 1;
      }
    }
    return cntTriplet;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 3;
    Console.WriteLine(findCntTriplet(N));
  }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

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