# Elements of Array which can be expressed as power of prime numbers

Given an array arr[] of size N, the task is to print all the elements of the Array which can be expressed as power of a prime number.

Examples:

Input: arr = {2, 8, 81, 36, 100}
Output: 2, 8, 81
Explanation:
Here 2 = 21, 8 = 23 and 81 = 34

Input: arr = {4, 7, 144}
Output: 4, 7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• The idea is to use Sieve of Eratosthenes and modify it to store all the exponent of prime numbers in a boolean array.
• Now traverse the given array and for each element check whether it is marked true or not in the boolean array.
• If marked true, Print the number.

Below is the implementation of the above approach:

## 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 `

Output:

```4 9 16 3 625
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.