কম্পিউটার

প্রাইম ফ্রিকোয়েন্সি সহ অ্যারে উপাদান?


ধরুন আমাদের একটি অ্যারে আছে। আমাদের গণনা করতে হবে কতগুলি উপাদান অ্যারে প্রাইম সংখ্যায় উপস্থিত রয়েছে। সুতরাং যদি অ্যারেটি হয় {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, তাহলে 1 বর্তমান 4 বার, 2 উপস্থিত 3 বার, 0 উপস্থিত 3 বার, এবং 5 বর্তমান 2 বার। সুতরাং তিনটি উপাদান আছে {2, 0, 5} যেগুলি মৌলিক সংখ্যার বার হয়েছে। সুতরাং গণনা হবে 3।

অ্যালগরিদম

countPrimeOccurrence(arr, n)

Begin
   count := 0
   define map with int type key and int type value
   for each element e in arr, do
      increase map.key(arr).value
   done
   for each key check whether the value corresponding the value is prime or not, if prime, then increase count.
   return count
End

উদাহরণ

#include <iostream>
#include <map>
using namespace std;
bool isPrime(int n){
   for(int i = 2; i<=n/2; i++){
      if(n % i == 0){
         return false;
      }
   }
   return true;
}
int countPrimeOcurrence(int arr[], int n){
   int count = 0;
   map<int, int> freq_map;
   for(int i = 0; i<n; i++){
      freq_map[arr[i]]++; //increase the frequency
   }
   for (auto it = freq_map.begin(); it != freq_map.end(); it++) {
      if (isPrime(it->second))
         count++;
   }
   return count;
}
int main() {
   int arr[] = {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout << "Prime frequency count: " << countPrimeOcurrence(arr, n);
}

আউটপুট

Prime frequency count: 3

  1. C++-এ অ্যারে উপাদানগুলির LCM-এর প্রাইম ফ্যাক্টর

  2. C++ এ প্রাইম ফ্রিকোয়েন্সি সহ অ্যারে উপাদান?

  3. পাইথনে অ্যারের সমস্ত উপাদানের ফ্রিকোয়েন্সি গণনা করুন

  4. উদাহরণ সহ ব্যাশ অ্যারে