📜  使用质数分解生成数字的所有除数

📅  最后修改于: 2021-05-05 02:48:45             🧑  作者: Mango

给定一个整数N ,任务是使用质数分解来找到所有除数。

例子:

方法:由于每个大于1的数字都可以在质数分解中表示为p 1 a 1 * p 2 a 2 *……* p k a k ,这里p i是质数,k≥1,a i是a正整数。
现在,如果知道每个素数n的出现次数,则可以递归生成所有可能的除数。对于每个素数因子p i ,可以将其包括x次,其中0≤x≤a i 。首先,使用这种方法找到n的素因式分解,并针对每个素因数,将其与它的出现次数一起存储。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include "iostream"
#include "vector"
using namespace std;
  
struct primeFactorization {
  
    // to store the prime factor
    // and its highest power
    int countOfPf, primeFactor;
};
  
// Recursive function to generate all the
// divisors from the prime factors
void generateDivisors(int curIndex, int curDivisor,
                      vector& arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.size()) {
        cout << curDivisor << ' ';
        return;
    }
  
    for (int i = 0; i <= arr[curIndex].countOfPf; ++i) {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr[curIndex].primeFactor;
    }
}
  
// Function to find the divisors of n
void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    vector arr;
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            int count = 0;
            while (n % i == 0) {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.push_back({ count, i });
        }
    }
  
    // If n is prime
    if (n > 1) {
        arr.push_back({ 1, n });
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
int main()
{
    int n = 6;
  
    findDivisors(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG 
{
  
static class primeFactorization 
{
  
    // to store the prime factor
    // and its highest power
    int countOfPf, primeFactor;
  
    public primeFactorization(int countOfPf, 
                              int primeFactor)
    {
        this.countOfPf = countOfPf;
        this.primeFactor = primeFactor;
    }
}
  
// Recursive function to generate all the
// divisors from the prime factors
static void generateDivisors(int curIndex, int curDivisor,
                           Vector arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.size()) 
    {
        System.out.print(curDivisor + " ");
        return;
    }
  
    for (int i = 0; i <= arr.get(curIndex).countOfPf; ++i)
    {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr.get(curIndex).primeFactor;
    }
}
  
// Function to find the divisors of n
static void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    Vector arr = new Vector<>();
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            int count = 0;
            while (n % i == 0) 
            {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.add(new primeFactorization(count, i ));
        }
    }
  
    // If n is prime
    if (n > 1)
    {
        arr.add(new primeFactorization( 1, n ));
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
public static void main(String []args) 
{
    int n = 6;
  
    findDivisors(n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Recursive function to generate all the 
# divisors from the prime factors 
def generateDivisors(curIndex, curDivisor, arr):
      
    # Base case i.e. we do not have more 
    # primeFactors to include 
    if (curIndex == len(arr)):
        print(curDivisor, end = ' ')
        return
      
    for i in range(arr[curIndex][0] + 1):
        generateDivisors(curIndex + 1, curDivisor, arr) 
        curDivisor *= arr[curIndex][1]
      
# Function to find the divisors of n 
def findDivisors(n):
      
    # To store the prime factors along 
    # with their highest power 
    arr = []
      
    # Finding prime factorization of n 
    i = 2
    while(i * i <= n):
        if (n % i == 0):
            count = 0
            while (n % i == 0):
                n //= i 
                count += 1
                  
            # For every prime factor we are storing 
            # count of it's occurenceand itself. 
            arr.append([count, i]) 
      
    # If n is prime 
    if (n > 1):
        arr.append([1, n]) 
      
    curIndex = 0
    curDivisor = 1
      
    # Generate all the divisors 
    generateDivisors(curIndex, curDivisor, arr) 
  
# Driver code 
n = 6
findDivisors(n) 
  
# This code is contributed by SHUBHAMSINGH10


C#
// C# implementation of the approach
using System; 
using System.Collections.Generic;
  
class GFG 
{
  
public class primeFactorization 
{
  
    // to store the prime factor
    // and its highest power
    public int countOfPf, primeFactor;
  
    public primeFactorization(int countOfPf, 
                              int primeFactor)
    {
        this.countOfPf = countOfPf;
        this.primeFactor = primeFactor;
    }
}
  
// Recursive function to generate all the
// divisors from the prime factors
static void generateDivisors(int curIndex, int curDivisor,
                             List arr)
{
  
    // Base case i.e. we do not have more
    // primeFactors to include
    if (curIndex == arr.Count) 
    {
        Console.Write(curDivisor + " ");
        return;
    }
  
    for (int i = 0; i <= arr[curIndex].countOfPf; ++i)
    {
        generateDivisors(curIndex + 1, curDivisor, arr);
        curDivisor *= arr[curIndex].primeFactor;
    }
}
  
// Function to find the divisors of n
static void findDivisors(int n)
{
  
    // To store the prime factors along
    // with their highest power
    List arr = new List();
  
    // Finding prime factorization of n
    for (int i = 2; i * i <= n; ++i)
    {
        if (n % i == 0)
        {
            int count = 0;
            while (n % i == 0) 
            {
                n /= i;
                count += 1;
            }
  
            // For every prime factor we are storing
            // count of it's occurenceand itself.
            arr.Add(new primeFactorization(count, i ));
        }
    }
  
    // If n is prime
    if (n > 1)
    {
        arr.Add(new primeFactorization( 1, n ));
    }
  
    int curIndex = 0, curDivisor = 1;
  
    // Generate all the divisors
    generateDivisors(curIndex, curDivisor, arr);
}
  
// Driver code
public static void Main(String []args) 
{
    int n = 6;
  
    findDivisors(n);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
1 3 2 6