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

📅  最后修改于: 2021-05-04 08:57:32             🧑  作者: Mango

给定一个整数N ,任务是从前N个自然数开始计算三元组( a,b,c ),以便a * b + c = N。

例子:

方法:可以通过以下观察来解决问题:

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

  • 初始化一个变量,例如cntTriplets ,以存储满足给定条件的前N个自然数的三元组的计数。
  • 使用变量i遍历[1,N – 1]范围并检查N%i == 0与否。如果发现是真的,则更新cntTriplets + =(N / i)– 1
  • 否则,更新cntTriplets + =(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


输出:
3

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