给定数字n ,请打印从1到n的所有数字的最小素数。整数n的最小素数因子是将数字除以最小的素数。所有偶数的最小素数是2。素数是它自己的最小素数(以及它自己的最大素数)。
注意:我们需要为1打印1。
例子 :
Input : 6
Output : Least Prime factor of 1: 1
Least Prime factor of 2: 2
Least Prime factor of 3: 3
Least Prime factor of 4: 2
Least Prime factor of 5: 5
Least Prime factor of 6: 2
我们可以使用Eratosthenes筛子的变体来解决上述问题。
- 创建一个从2到n的连续整数列表:(2,3,4,…,n)。
- 最初,让我等于2,最小的质数。
- 通过以i的增量从2i计数到n来枚举i的倍数,并将它们标记为具有最小素数的i(如果尚未标记)。还要将i标记为i的最小质数(i本身是质数)。
- 在未标记的列表中找到大于i的第一个数字。如果没有这样的数字,请停止。否则,让我现在等于这个新数字(这是下一个质数),然后从步骤3开始重复。
下面是该算法的实现,其中minimum_prime []保存与各个索引相对应的最小素数因子的值。
C++
// C++ program to print the least prime factors
// of numbers less than or equal to
// n using modified Sieve of Eratosthenes
#include
using namespace std;
void leastPrimeFactor(int n)
{
// Create a vector to store least primes.
// Initialize all entries as 0.
vector least_prime(n+1, 0);
// We need to print 1 for 1.
least_prime[1] = 1;
for (int i = 2; i <= n; i++)
{
// least_prime[i] == 0
// means it i is prime
if (least_prime[i] == 0)
{
// marking the prime number
// as its own lpf
least_prime[i] = i;
// mark it as a divisor for all its
// multiples if not already marked
for (int j = i*i; j <= n; j += i)
if (least_prime[j] == 0)
least_prime[j] = i;
}
}
// print least prime factor of
// of numbers till n
for (int i = 1; i <= n; i++)
cout << "Least Prime factor of "
<< i << ": " << least_prime[i] << "\n";
}
// Driver program to test above function
int main()
{
int n = 10;
leastPrimeFactor(n);
return 0;
}
Java
// Java program to print the least prime factors
// of numbers less than or equal to
// n using modified Sieve of Eratosthenes
import java.io.*;
import java.util.*;
class GFG
{
public static void leastPrimeFactor(int n)
{
// Create a vector to store least primes.
// Initialize all entries as 0.
int[] least_prime = new int[n+1];
// We need to print 1 for 1.
least_prime[1] = 1;
for (int i = 2; i <= n; i++)
{
// least_prime[i] == 0
// means it i is prime
if (least_prime[i] == 0)
{
// marking the prime number
// as its own lpf
least_prime[i] = i;
// mark it as a divisor for all its
// multiples if not already marked
for (int j = i*i; j <= n; j += i)
if (least_prime[j] == 0)
least_prime[j] = i;
}
}
// print least prime factor of
// of numbers till n
for (int i = 1; i <= n; i++)
System.out.println("Least Prime factor of " +
+ i + ": " + least_prime[i]);
}
public static void main (String[] args)
{
int n = 10;
leastPrimeFactor(n);
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
Python 3
# Python 3 program to print the
# least prime factors of numbers
# less than or equal to n using
# modified Sieve of Eratosthenes
def leastPrimeFactor(n) :
# Create a vector to store least primes.
# Initialize all entries as 0.
least_prime = [0] * (n + 1)
# We need to print 1 for 1.
least_prime[1] = 1
for i in range(2, n + 1) :
# least_prime[i] == 0
# means it i is prime
if (least_prime[i] == 0) :
# marking the prime number
# as its own lpf
least_prime[i] = i
# mark it as a divisor for all its
# multiples if not already marked
for j in range(i * i, n + 1, i) :
if (least_prime[j] == 0) :
least_prime[j] = i
# print least prime factor
# of numbers till n
for i in range(1, n + 1) :
print("Least Prime factor of "
,i , ": " , least_prime[i] )
# Driver program
n = 10
leastPrimeFactor(n)
# This code is contributed
# by Nikita Tiwari.
C#
// C# program to print the least prime factors
// of numbers less than or equal to
// n using modified Sieve of Eratosthenes
using System;
class GFG
{
public static void leastPrimeFactor(int n)
{
// Create a vector to store least primes.
// Initialize all entries as 0.
int []least_prime = new int[n+1];
// We need to print 1 for 1.
least_prime[1] = 1;
for (int i = 2; i <= n; i++)
{
// least_prime[i] == 0
// means it i is prime
if (least_prime[i] == 0)
{
// marking the prime number
// as its own lpf
least_prime[i] = i;
// mark it as a divisor for all its
// multiples if not already marked
for (int j = i*i; j <= n; j += i)
if (least_prime[j] == 0)
least_prime[j] = i;
}
}
// print least prime factor of
// of numbers till n
for (int i = 1; i <= n; i++)
Console.WriteLine("Least Prime factor of " +
i + ": " + least_prime[i]);
}
// Driver code
public static void Main ()
{
int n = 10;
// Function calling
leastPrimeFactor(n);
}
}
// This code is contributed by Nitin Mittal
PHP
Javascript
输出
Least Prime factor of 1: 1
Least Prime factor of 2: 2
Least Prime factor of 3: 3
Least Prime factor of 4: 2
Least Prime factor of 5: 5
Least Prime factor of 6: 2
Least Prime factor of 7: 7
Least Prime factor of 8: 2
Least Prime factor of 9: 3
Least Prime factor of 10: 2