📜  打印给定数字的所有质因数的高效程序

📅  最后修改于: 2021-09-17 06:45:49             🧑  作者: Mango

给定一个数 n,编写一个有效的函数来打印 n 的所有质因数。例如,如果输入数为 12,则输出应为“2 2 3”。如果输入数为 315,则输出应为“3 3 5 7”。

以下是查找所有质因数的步骤。
1)当 n 可被 2 整除时,打印 2 并将 n 除以 2。
2)在第 1 步之后,n 必须是奇数。现在开始从 i = 3 到 n 的平方根的循环。当 i 除以 n 时,打印 i 并将 n 除以 i。在 i 除以 n 失败后,将 i 增加 2 并继续。
3)如果n是一个质数并且大于2,那么经过以上两步n就不会变成1。因此,如果 n 大于 2,则打印 n。

C++
// C++ program to print all prime factors
#include 
using namespace std;
 
// A function to print all prime
// factors of a given number n
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }
 
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }
 
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        cout << n << " ";
}
 
/* Driver code */
int main()
{
    int n = 315;
    primeFactors(n);
    return 0;
}
 
// This is code is contributed by rathbhupendra


C
// Program to print all prime factors
# include 
# include 
 
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n%2 == 0)
    {
        printf("%d ", 2);
        n = n/2;
    }
 
    // n must be odd at this point.  So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i+2)
    {
        // While i divides n, print i and divide n
        while (n%i == 0)
        {
            printf("%d ", i);
            n = n/i;
        }
    }
 
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        printf ("%d ", n);
}
 
/* Driver program to test above function */
int main()
{
    int n = 315;
    primeFactors(n);
    return 0;
}


Java
// Program to print all prime factors
import java.io.*;
import java.lang.Math;
 
class GFG
{
    // A function to print all prime factors
    // of a given number n
    public static void primeFactors(int n)
    {
        // Print the number of 2s that divide n
        while (n%2==0)
        {
            System.out.print(2 + " ");
            n /= 2;
        }
 
        // n must be odd at this point.  So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.sqrt(n); i+= 2)
        {
            // While i divides n, print i and divide n
            while (n%i == 0)
            {
                System.out.print(i + " ");
                n /= i;
            }
        }
 
        // This condition is to handle the case whien
        // n is a prime number greater than 2
        if (n > 2)
            System.out.print(n);
    }
 
    public static void main (String[] args)
    {
        int n = 315;
        primeFactors(n);
    }
}


Python
# Python program to print prime factors
 
import math
 
# A function to print all prime factors of
# a given number n
def primeFactors(n):
     
    # Print the number of two's that divide n
    while n % 2 == 0:
        print 2,
        n = n / 2
         
    # n must be odd at this point
    # so a skip of 2 ( i = i + 2) can be used
    for i in range(3,int(math.sqrt(n))+1,2):
         
        # while i divides n , print i ad divide n
        while n % i== 0:
            print i,
            n = n / i
             
    # Condition if n is a prime
    # number greater than 2
    if n > 2:
        print n
         
# Driver Program to test above function
 
n = 315
primeFactors(n)
 
# This code is contributed by Harshit Agrawal


C#
// C# Program to print all prime factors
using System;
 
namespace prime
{
public class GFG
{    
                 
    // A function to print all prime
    // factors of a given number n
    public static void primeFactors(int n)
    {
        // Print the number of 2s that divide n
        while (n % 2 == 0)
        {
            Console.Write(2 + " ");
            n /= 2;
        }
 
        // n must be odd at this point. So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.Sqrt(n); i+= 2)
        {
            // While i divides n, print i and divide n
            while (n % i == 0)
            {
                Console.Write(i + " ");
                n /= i;
            }
        }
 
        // This condition is to handle the case whien
        // n is a prime number greater than 2
        if (n > 2)
            Console.Write(n);
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 315;
        primeFactors(n);
    }
 
}
}
 
// This code is contributed by Sam007


PHP
 2)
        echo $n," ";
}
 
    // Driver Code
    $n = 315;
    primeFactors($n);
 
// This code is contributed by aj_36
?>


Javascript


输出:

3 3 5 7

这是如何运作的?
步骤 1 和 2 处理合数,步骤 3 处理素数。为了证明完整的算法有效,我们需要证明第 1 步和第 2 步实际上处理的是合数。很明显,第 1 步处理的是偶数。并且在第 1 步之后,所有剩余的质因数必须是奇数(两个质因数的差必须至少为 2),这就解释了为什么 i 增加了 2。

现在的主要部分是,循环运行到 n 的平方根,而不是 n。为了证明这种优化有效,让我们考虑合数的以下性质。

每个合数至少有一个小于或等于其平方根的质因数。
这个性质可以用 counter 语句证明。设 a 和 b 是 n 的两个因数,使得 a*b = n。如果两者都大于 √n,则 ab > √n, * √n,这与表达式“a * b = n”相矛盾。

在上述算法的第 2 步中,我们运行一个循环并在循环中执行以下操作
a) 找到最小的质因子 i(必须小于 √n,)
b) 通过重复将 n 除以 i,从 n 中删除所有出现的 i。
c) 对除数 n 和 i = i + 2 重复步骤 a 和 b。重复步骤 a 和 b,直到 n 变为 1 或素数。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程