📜  给定数字的非质数除数的计数

📅  最后修改于: 2021-06-26 01:40:13             🧑  作者: Mango

给定数字N ,任务是查找给定数字N的非素数除数。
例子:

方法:对该问题的主要观察是,任何数字都可以写为其主要因素的乘积,例如

N=\prod_{i=1}^Ka_{i}^{b_i}

其中K是给定数的素数的计数。使用排列和组合,我们可以发现
的因素是

\prod_{i=1}^k{(b_i + 1)}

因此,非主要因素的计数为:

\text{Count of Non-prime factors = } \prod_{i=1}^k{(b_i + 1)} - k

下面是上述方法的实现:

C++
// C++ program to find count of
// non-prime divisors of given number
 
#include 
using namespace std;
 
// Function to factors of the given
// number
vector getFactorization(int x)
{
    int count = 0;
    vector v;
 
    // Loop to find the divisors of
    // the number 2
    while (x % 2 == 0) {
        count++;
        x = x / 2;
    }
    if (count != 0)
        v.push_back(count);
 
    // Loop to find the divisors of the
    // given number upto SQRT(N)
    for (int i = 3; i <= sqrt(x); i += 2) {
        count = 0;
        while (x % i == 0) {
            count++;
            x /= i;
        }
        if (count != 0)
            v.push_back(count);
    }
 
    // Condition to check if the rest
    // number is also a prime number
    if (x > 1) {
        v.push_back(1);
    }
    return v;
}
 
// Function to find the non-prime
// divisors of the given number
int nonPrimeDivisors(int N)
{
    vector v = getFactorization(N);
    int ret = 1;
 
    // Loop to count the number of
    // the total divisors of given number
    for (int i = 0; i < v.size(); i++)
        ret = ret * (v[i] + 1);
 
    ret = ret - v.size();
    return ret;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    // Function Call
    cout << nonPrimeDivisors(N) << endl;
    return 0;
}


Java
// Java program to find
// count of non-prime
// divisors of given number
import java.util.*;
class GFG{
 
// Function to factors
// of the given number
static Vector getFactorization(int x)
{
  int count = 0;
  Vector v = new Vector<>();
 
  // Loop to find the
  // divisors of the number 2
  while (x % 2 == 0)
  {
    count++;
    x = x / 2;
  }
   
  if (count != 0)
    v.add(count);
 
  // Loop to find the divisors
  // of the given number upto SQRT(N)
  for (int i = 3;
           i <= Math.sqrt(x); i += 2)
  {
    count = 0;
    while (x % i == 0)
    {
      count++;
      x /= i;
    }
     
    if (count != 0)
      v.add(count);
  }
 
  // Condition to check if
  // the rest number is also
  // a prime number
  if (x > 1)
  {
    v.add(1);
  }
  return v;
}
 
// Function to find the non-prime
// divisors of the given number
static int nonPrimeDivisors(int N)
{
  Vector v = getFactorization(N);
  int ret = 1;
 
  // Loop to count the number of
  // the total divisors of given number
  for (int i = 0; i < v.size(); i++)
    ret = ret * (v.get(i) + 1);
 
  ret = ret - v.size();
  return ret;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 8;
 
  // Function Call
  System.out.println(nonPrimeDivisors(N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to find count of
# non-prime divisors of given number
from math import sqrt
 
# Function to factors of the given
# number
def getFactorization(x):
     
    count = 0
    v = []
 
    # Loop to find the divisors of
    # the number 2
    while (x % 2 == 0):
        count += 1
        x = x // 2
 
    if (count != 0):
        v.append(count)
 
    # Loop to find the divisors of the
    # given number upto SQRT(N)
    for i in range(3, int(sqrt(x)) + 12):
        count = 0
         
        while (x % i == 0):
            count += 1
            x //= i
             
        if (count != 0):
            v.append(count)
 
    # Condition to check if the rest
    # number is also a prime number
    if (x > 1):
        v.append(1)
         
    return v
 
# Function to find the non-prime
# divisors of the given number
def nonPrimeDivisors(N):
     
    v = getFactorization(N)
    ret = 1
 
    # Loop to count the number of
    # the total divisors of given number
    for i in range(len(v)):
        ret = ret * (v[i] + 1)
    ret = ret - len(v)
     
    return ret
 
# Driver Code
if __name__ == '__main__':
     
    N = 8
 
    # Function Call
    print(nonPrimeDivisors(N))
 
# This code is contributed by Samarth


C#
// C# program to find
// count of non-prime
// divisors of given number
using System;
using System.Collections.Generic;
class GFG{
 
// Function to factors
// of the given number
static List getFactorization(int x)
{
  int count = 0;
  List v = new List();
 
  // Loop to find the
  // divisors of the number 2
  while (x % 2 == 0)
  {
    count++;
    x = x / 2;
  }
   
  if (count != 0)
    v.Add(count);
 
  // Loop to find the divisors
  // of the given number upto
  // SQRT(N)
  for (int i = 3;
           i <= Math.Sqrt(x); i += 2)
  {
    count = 0;
    while (x % i == 0)
    {
      count++;
      x /= i;
    }
     
    if (count != 0)
      v.Add(count);
  }
 
  // Condition to check if
  // the rest number is also
  // a prime number
  if (x > 1)
  {
    v.Add(1);
  }
  return v;
}
 
// Function to find the non-prime
// divisors of the given number
static int nonPrimeDivisors(int N)
{
  List v = getFactorization(N);
  int ret = 1;
 
  // Loop to count the number of
  // the total divisors of given number
  for (int i = 0; i < v.Count; i++)
    ret = ret * (v[i] + 1);
 
  ret = ret - v.Count;
  return ret;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 8;
 
  // Function Call
  Console.WriteLine(nonPrimeDivisors(N));
}
}
 
// This code is contributed by gauravrajput1


输出:
3