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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr = {2, 8, 81, 36, 100}
Output: 2, 8, 81
Explanation:
Here 2 = 21, 8 = 23 and 81 = 34Input: arr = {4, 7, 144}
Output: 4, 7
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 <bits/stdc++.h>using namespace std;// Function to mark all the// exponent of prime numbersvoid 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 elementsvoid 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 numbersvoid 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 codeint 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 numbersimport java.util.*;class GFG{ // Function to mark all the// exponent of prime numbersstatic 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 elementsstatic 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 numbersstatic 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 codepublic 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 numbersdef 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 elementsdef Display(arr, Expo_Prime, n) : for i in range(n) : if (Expo_Prime[arr[i]]) : print(arr[i], end= " ");# Function to print the required numbersdef 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 codeif __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 numbersusing System;class GFG{// Function to mark all the// exponent of prime numbersstatic 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// elementsstatic 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 numbersstatic 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 codepublic 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
<script>// JavaScript program to print all elements// of Array which can be expressed// as power of prime numbers// Function to mark all the// exponent of prime numbersfunction ModifiedSieveOfEratosthenes(N, Expo_Prime){ let primes = new Array(N); primes.fill(true) for (let i = 2; i < N; i++) { if (primes[i]) { let no = i; // If number is prime then marking // all of its exponent true while (no <= N) { Expo_Prime[no] = true; no *= i; } for (let j = i * i; j < N; j += i) primes[j] = false; } }}// Function to display all required elementsfunction Display(arr, Expo_Prime, n){ for (let i = 0; i < n; i++) if (Expo_Prime[arr[i]]) document.write(arr[i] + " ");}// Function to print the required numbersfunction FindExpoPrime(arr, n){ let max = 0; // To find the largest number for (let i = 0; i < n; i++) { if (max < arr[i]) max = arr[i]; } let Expo_Prime = new Array(max + 1); Expo_Prime.fill(false) // Function call to mark all the // Exponential prime nos. ModifiedSieveOfEratosthenes( max + 1, Expo_Prime); // Function call Display(arr, Expo_Prime, n);}// Driver code let arr = [ 4, 6, 9, 16, 1, 3, 12, 36, 625, 1000 ]; let n = arr.length FindExpoPrime(arr, n);// This code is contributed by gfgking</script> |
4 9 16 3 625

