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

📅  最后修改于: 2021-10-27 07:19:55             🧑  作者: Mango

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

例子:

方法:

  • 这个想法是使用埃拉托色尼筛法并修改它以将素数的所有指数存储在一个布尔数组中。
  • 现在遍历给定的数组,并为每个元素检查它在布尔数组中是否被标记为真。
  • 如果标记为真,则打印数字。

下面是上述方法的实现:

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 display 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 display 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 display 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 display 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


Javascript


输出:
4 9 16 3 625

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