📌  相关文章
📜  可以表示为素数幂的数组元素

📅  最后修改于: 2021-04-26 09:37:33             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是打印可以表示为质数幂的Array的所有元素。

例子:

方法:

  • 这个想法是使用Eratosthenes的Sieve并对其进行修改,以将所有质数指数存储在布尔数组中。
  • 现在遍历给定的数组,并检查每个元素在布尔数组中是否标记为true。
  • 如果标记为true,则打印号码。

下面是上述方法的实现:

C++
// C++ program to print all elements
// of Array which can be expressed
// as power of prime numbers
  
#include 
using namespace std;
  
// Function to mark all the
// exponent of prime numbers
void ModifiedSieveOfEratosthenes(
    int N, bool Expo_Prime[])
{
    bool primes[N];
    memset(primes, true, sizeof(primes));
  
    for (int i = 2; i < N; i++) {
  
        if (primes[i]) {
  
            int no = i;
  
            // If number is prime then marking
            // all of its exponent true
            while (no <= N) {
  
                Expo_Prime[no] = true;
                no *= i;
            }
  
            for (int j = i * i; j < N; j += i)
                primes[j] = false;
        }
    }
}
  
// Function to diplay all required elements
void Display(int arr[],
             bool Expo_Prime[],
             int n)
{
  
    for (int i = 0; i < n; i++)
        if (Expo_Prime[arr[i]])
            cout << arr[i] << " ";
}
  
// Function to print the required numbers
void FindExpoPrime(int arr[], int n)
{
    int max = 0;
  
    // To find the largest number
    for (int i = 0; i < n; i++) {
        if (max < arr[i])
            max = arr[i];
    }
  
    bool Expo_Prime[max + 1];
  
    memset(Expo_Prime, false,
           sizeof(Expo_Prime));
  
    // Function call to mark all the
    // Exponential prime nos.
    ModifiedSieveOfEratosthenes(
        max + 1, Expo_Prime);
  
    // Function call
    Display(arr, Expo_Prime, n);
}
  
// Driver code
int main()
{
    int arr[] = { 4, 6, 9, 16, 1, 3,
                  12, 36, 625, 1000 };
    int n = sizeof(arr) / sizeof(int);
  
    FindExpoPrime(arr, n);
  
    return 0;
}


Java
// Java program to print all elements
// of Array which can be expressed
// as power of prime numbers
import java.util.*;
  
class GFG{
   
// Function to mark all the
// exponent of prime numbers
static void ModifiedSieveOfEratosthenes(
    int N, boolean Expo_Prime[])
{
    boolean []primes = new boolean[N];
    Arrays.fill(primes, true);
   
    for (int i = 2; i < N; i++) {
   
        if (primes[i]) {
   
            int no = i;
   
            // If number is prime then marking
            // all of its exponent true
            while (no <= N) {
   
                Expo_Prime[no] = true;
                no *= i;
            }
   
            for (int j = i * i; j < N; j += i)
                primes[j] = false;
        }
    }
}
   
// Function to diplay all required elements
static void Display(int arr[],
             boolean Expo_Prime[],
             int n)
{
   
    for (int i = 0; i < n; i++)
        if (Expo_Prime[arr[i]])
            System.out.print(arr[i]+ " ");
}
   
// Function to print the required numbers
static void FindExpoPrime(int arr[], int n)
{
    int max = 0;
   
    // To find the largest number
    for (int i = 0; i < n; i++) {
        if (max < arr[i])
            max = arr[i];
    }
   
    boolean []Expo_Prime = new boolean[max + 1];
  
   
    // Function call to mark all the
    // Exponential prime nos.
    ModifiedSieveOfEratosthenes(
        max + 1, Expo_Prime);
   
    // Function call
    Display(arr, Expo_Prime, n);
}
   
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 6, 9, 16, 1, 3,
                  12, 36, 625, 1000 };
    int n = arr.length;
   
    FindExpoPrime(arr, n);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program to print all elements 
# of Array which can be expressed 
# as power of prime numbers 
  
# Function to mark all the 
# exponent of prime numbers 
def ModifiedSieveOfEratosthenes(N, Expo_Prime) :
      
    primes = [True]*N; 
  
    for i in range(2, N) :
        if (primes[i]) :
  
            no = i; 
  
            # If number is prime then marking 
            # all of its exponent true 
            while (no <= N) :
  
                Expo_Prime[no] = True; 
                no *= i; 
  
            for j in range(i * i, N, i) : 
                primes[j] = False; 
      
# Function to diplay all required elements 
def Display(arr, Expo_Prime, n) : 
  
    for i in range(n) :
        if (Expo_Prime[arr[i]]) :
            print(arr[i], end= " ");
  
# Function to print the required numbers 
def FindExpoPrime(arr, n) : 
  
    max = 0; 
  
    # To find the largest number 
    for i in range(n) :
        if (max < arr[i]) :
            max = arr[i]; 
  
    Expo_Prime = [False]*(max + 1); 
  
    # Function call to mark all the 
    # Exponential prime nos. 
    ModifiedSieveOfEratosthenes(max + 1, Expo_Prime); 
  
    # Function call 
    Display(arr, Expo_Prime, n); 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 4, 6, 9, 16, 1, 3, 
                12, 36, 625, 1000 ]; 
    n = len(arr); 
  
    FindExpoPrime(arr, n); 
  
# This code is contributed by Yash_R


C#
// C# program to print all elements
// of Array which can be expressed
// as power of prime numbers
using System;
  
class GFG{
  
// Function to mark all the
// exponent of prime numbers
static void ModifiedSieveOfEratosthenes(int N,
                            bool []Expo_Prime)
{
    bool []primes = new bool[N];
    for(int i = 0; i < N; i++)
        primes[i] = true;
          
    for(int i = 2; i < N; i++)
    {
       if (primes[i])
       {
           int no = i;
             
           // If number is prime then marking
           // all of its exponent true
           while (no <= N)
           {
               Expo_Prime[no] = true;
               no *= i;
           }
           for(int j = i * i; j < N; j += i)
              primes[j] = false;
       }
    }
}
  
// Function to diplay all required
// elements
static void Display(int []arr,
                    bool []Expo_Prime,
                    int n)
{
    for(int i = 0; i < n; i++)
       if (Expo_Prime[arr[i]])
           Console.Write(arr[i] + " ");
}
  
// Function to print the required numbers
static void FindExpoPrime(int []arr, int n)
{
    int max = 0;
  
    // To find the largest number
    for(int i = 0; i < n; i++) 
    {
       if (max < arr[i])
           max = arr[i];
    }
  
    bool []Expo_Prime = new bool[max + 1];
  
    // Function call to mark all the
    // Exponential prime nos.
    ModifiedSieveOfEratosthenes(max + 1,
                                Expo_Prime);
                                  
    // Function call
    Display(arr, Expo_Prime, n);
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 4, 6, 9, 16, 1, 3,
                  12, 36, 625, 1000 };
    int n = arr.Length;
  
    FindExpoPrime(arr, n);
}
}
  
// This code is contributed by Princi Singh


输出:
4 9 16 3 625