给定数字n,编写一个有效函数以打印n的所有素数。例如,如果输入数字为12,则输出应为“ 2 2 3”。如果输入数字为315,则输出应为“ 3 3 5 7”。
以下是查找所有主要因素的步骤。
1)当n被2整除时,打印2并将n除以2。
2)步骤1之后,n必须为奇数。现在从i = 3到n的平方根开始循环。当我除以n时,打印i并将n除以i。在我未能将n除以后,将i加2并继续。
3)如果n是质数且大于2,那么经过以上两个步骤,n不会变为1。因此,如果大于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
?>
输出:
3 3 5 7
这是如何运作的?
步骤1和2负责合成数字,而步骤3负责质数。为了证明完整的算法有效,我们需要证明步骤1和2实际上处理了复合数。显然,第1步要处理偶数。在第1步之后,所有剩余质数必须为奇数(两个质数之差必须至少为2),这解释了为什么我要加2。
现在最主要的是,循环一直运行到n的平方根,直到n为止。为了证明这种优化有效,让我们考虑以下合成数字的属性。
每个复合数至少具有一个小于或等于其平方根的素数。
可以使用计数器语句证明此属性。令a和b为n的两个因数,使得a * b = n。如果两者均大于&Sqrt; n,则ab>&Sqrt; n,*&Sqrt; n,这与表达式“ a * b = n”相矛盾。
在上述算法的第2步中,我们运行一个循环并在循环中进行后续操作
a)找到最小素数因子i(必须小于&Sqrt; n,)
b)通过将n重复除以i,从n中删除所有出现的i。
c)重复步骤a和b,除以n,i = i +2。重复步骤a和b,直到n变为1或质数。
相关文章:
使用Sieve O(log n)进行质因子分解以进行多个查询