📜  给定两个乘积为N的因子的位置时,查找N的因子数

📅  最后修改于: 2021-04-27 17:16:35             🧑  作者: Mango

给定ab表示两个因子N的位置,当两个因子按升序排列时,其乘积等于数N。任务是找到因子N的总数。
例子:

方法:该解决方案基于观察。
假设N = 50 。那么N有6个因数1、2、5、10、25和50。将1和50相乘将始终得到值50(N)。同样,通过将2和25相乘得到N值,类似地通过将5和10相乘得到N值。因此,在这里我们可以看到,当两个因子按升序排列时,相乘得出N值。以一种方式完成:乘法的第一个因子和最后一个因子为N,乘法的第二个因子和第二个最后一个因子为N,依此类推。
通过这种模式,我们可以找到一种计算因子数量的方法。假设乘法的第1个和第4个因子给出N。这意味着有4个因子(第1个,第2个,第3个和第4个)。如果第二和第三因子的乘积给出N,那么我们可以说在第一位置和第四位置必须有一个因子。
因此,因子的数量将等于a + b – 1
下面是上述方法的实现:

C++
// C++ program to implement
// the above problem
#include 
using namespace std;
 
// Function to find the number of factors
void findFactors(int a, int b)
{
    int c;
    c = a + b - 1;
 
    // print the number of factors
    cout << c;
}
 
// Driver code
int main()
{
    // initialize the factors position
    int a, b;
    a = 13;
    b = 36;
 
    findFactors(a, b);
 
    return 0;
}


Java
// Java program to implement
// the above problem
class GFG
{
 
// Function to find the number of factors
static void findFactors(int a, int b)
{
    int c;
    c = a + b - 1;
 
    // print the number of factors
    System.out.print(c);
}
 
// Driver code
public static void main(String[] args)
{
    // initialize the factors position
    int a, b;
    a = 13;
    b = 36;
 
    findFactors(a, b);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python 3 program to implement
# the above problem
 
# Function to find the number of factors
def findFactors(a, b):
    c = a + b - 1
 
    # print the number of factors
    print(c)
 
# Driver code
if __name__ == '__main__':
     
    # initialize the factors position
    a = 13
    b = 36
    findFactors(a, b)
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to implement
// the above problem
using System;
     
class GFG
{
 
// Function to find the number of factors
static void findFactors(int a, int b)
{
    int c;
    c = a + b - 1;
 
    // print the number of factors
    Console.Write(c);
}
 
// Driver code
public static void Main(String[] args)
{
    // initialize the factors position
    int a, b;
    a = 13;
    b = 36;
 
    findFactors(a, b);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
48

时间复杂度: O(1)