📜  将数字表示为两个不同因素的乘积的方式

📅  最后修改于: 2021-04-27 18:19:08             🧑  作者: Mango

给定一个数字n,编写一个程序来计算将数字表示为两个不同因子的乘积的方式。

例子 :

Input : 12
Output : 3
12 can be expressed as 1 * 12, 2 * 6 and 3*4. 

Input : 36
Output : 4
36 can be expressed as 1 * 36, 2 * 18, 3 * 12 and 4 * 9.
All factors of 12 are = 1, 2, 3, 4, 6, 12

We can observe that factors always exist in
pair which is equal to number.

Here (1, 12), (2, 6) and (3, 4) are such pairs.

由于数字可以表示为两个因子的乘积,因此我们只需要找到数字的因子数直至数字的平方根即可。而且我们只需要找到不同的对,因此在完美平方的情况下,我们不包括该因素。

C++
// CPP program to find number of ways
// in which number expressed as
// product of two different factors
#include 
using namespace std;
  
// To count number of ways in which
// number expressed as product
// of two different numbers
int countWays(int n)
{
    // To store count of such pairs
    int count = 0;
  
    // Counting number of pairs
    // upto sqrt(n) - 1
    for (int i = 1; i * i < n; i++)
        if (n % i == 0)
            count++;
  
    // To return count of pairs
    return count;
}
  
// Driver program to test countWays()
int main()
{
    int n = 12;
    cout << countWays(n) << endl;
    return 0;
}


Java
// Java program to find number of ways
// in which number expressed as
// product of two different factors
public class Main {
  
    // To count number of ways in which
    // number expressed as product
    // of two different numbers
    static int countWays(int n)
    {
        // To store count of such pairs
        int count = 0;
  
        // Counting number of pairs
        // upto sqrt(n) - 1
        for (int i = 1; i * i < n; i++)
            if (n % i == 0)
                count++;
  
        // To return count of pairs
        return count;
    }
  
    // Driver program to test countWays()
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(countWays(n));
    }
}


Python 3
# Python 3 program to find number of ways
# in which number expressed as
# product of two different factors
  
# To count number of ways in which
# number expressed as product
# of two different numbers
def countWays(n):
      
    # To store count of such pairs
    count = 0
    i = 1
      
    # Counting number of pairs
    # upto sqrt(n) - 1
    while ((i * i)


C#
// C# program to find number of ways
// in which number expressed as
// product of two different factors
using System;
  
public class main {
  
    // To count number of ways in which
    // number expressed as product
    // of two different numbers
    static int countWays(int n)
    {
  
        // To store count of such pairs
        int count = 0;
  
        // Counting number of pairs
        // upto sqrt(n) - 1
        for (int i = 1; i * i < n; i++)
            if (n % i == 0)
                count++;
  
        // To return count of pairs
        return count;
    }
  
    // Driver program to test countWays()
    public static void Main()
    {
        int n = 12;
  
        Console.WriteLine(countWays(n));
    }
}
  
// This code is contributed by Anant Agarwal.


PHP


输出 :

3