📜  范围内的K-Prime(具有k个素因数的数字)

📅  最后修改于: 2021-05-04 15:06:23             🧑  作者: Mango

给定三个整数A,B和K。我们需要找到否。 [A,B]范围内的K素数的取值。如果一个数字恰好具有K个不同的素数,则称为K-素数。

例子:

Input : A = 4, B = 10, K = 2.
Output : 6 10
Given range is [4, 5, 6, 7, 8, 9, 10].
From the above range 6 and 10 have 2 distinct 
prime factors, 6 = 3*2; 10 = 5*2.

Input : A = 14, B = 18, K = 2.
Output : 14 15 18
Range = [14, 15].
Both 14, 15 and 18 have 2 distinct prime factors,
14 = 7*2, 15 = 3*5 and 18 = 2*3*3

一个简单的解决方案是遍历给定范围。对于范围的每个元素,请找到其主要因素。最后打印所有素数为k的数字。

一个有效的解决方案是使用筛网算法

prime[n] = {true};
for (int p=2; p*p<=n; p++)
{
   // If prime[p] is not changed, then 
   // it is a prime
   if (prime[p] == true)
   {
      // Update all multiples of p
      for (int i=p*2; i<=n; i += p)
         prime[i] = false;
    }
}
 

如果我们清楚地观察到上述算法,它具有遍历小于N的所有质数倍数的特性。因此,该算法标记一个非质数的次数等于该数的质因数。为此,请维护一个称为标记的数组,并在每次算法将其标记为非质数时增加其计数。下一步,我们迭代[A,B]范围内的所有数字,并在标记为[number] == K的情况下增加k个素数的计数。

C++
// CPP program to count all those numbers in
// given range whose count of prime factors
// is k
#include 
using namespace std;
 
void printKPFNums(int A, int B, int K)
{
    // Count prime factors of all numbers
    // till B.
    bool prime[B+1] = { true };
    int p_factors[B+1] = { 0 };
    for (int p = 2; p <= B; p++)
        if (p_factors[p] == 0)
            for (int i = p; i <= B; i += p)
                p_factors[i]++;
 
    // Print all numbers with k prime factors
    for (int i = A; i <= B; i++)
        if (p_factors[i] == K)
            cout << i << " ";
}
 
// Driver code
int main()
{
    int A = 14, B = 18, K = 2;
    printKPFNums(A, B, K);
    return 0;
}


Java
// Java program to count
// all those numbers in
// given range whose count
// of prime factors
// is k
 
import java.io.*;
import java.util.*;
 
class GFG {
     
    static void printKPFNums(int A, int B, int K)
    {
        // Count prime factors of all numbers
        // till B.
        int p_factors[] = new int[B+1];
        Arrays.fill(p_factors,0);
 
        for (int p = 2; p <= B; p++)
            if (p_factors[p] == 0)
                for (int i = p; i <= B; i += p)
                    p_factors[i]++;
      
        // Print all numbers with k prime factors
        for (int i = A; i <= B; i++)
            if (p_factors[i] == K)
                System.out.print( i + " ");
    }
      
    // Driver code
    public static void main(String args[])
    {
        int A = 14, B = 18, K = 2;
        printKPFNums(A, B, K);
    }
}
 
 
// This code is contributed
// by Nikita Tiwari.


Python3
# Python 3 program to count
# all those numbers in
# given range whose count
# of prime factors
# is k
 
def printKPFNums(A, B, K) :
 
    # Count prime factors
    # of all numbers
    # till B.
    prime = [ True]*(B+1)
    p_factors= [ 0 ]*(B+1)
    for p in range(2,B+1) :
        if (p_factors[p] == 0)  :
            for i in range(p,B+1,p) :
                p_factors[i] = p_factors[i] + 1
  
    # Print all numbers with
    # k prime factors
    for i in range(A,B+1) :
        if (p_factors[i] == K) :
            print( i ,end=" ")
 
 
# Driver code
A = 14
B = 18
K = 2
printKPFNums(A, B, K)
 
 
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to count all
// those numbers in given
// range whose count of
// prime factors is k
using System;
 
class GFG {
     
    static void printKPFNums(int A, int B,
                                    int K)
    {
        // Count prime factors of
        // all numbers till B.
        bool []prime = new bool[B + 1];
         
        for(int i = 0; i < B + 1; i++)
            prime[i] = true;
             
        int []p_factors = new int[B + 1];
         
        for(int i = 0; i < B + 1; i++)
            p_factors[i] = 0;
 
        for (int p = 2; p <= B; p++)
            if (p_factors[p] == 0)
                for (int i = p; i <= B; i += p)
                    p_factors[i]++;
     
        // Print all numbers with
        // k prime factors
        for (int i = A; i <= B; i++)
            if (p_factors[i] == K)
                Console.Write( i + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int A = 14, B = 18, K = 2;
        printKPFNums(A, B, K);
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出:

14 15 18