Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. // C++ program to generate all prime numbers
  2. // less than N in O(N)
  3. #include<bits/stdc++.h>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. static int NumUnos(int n) {
  12.    
  13.     int cont = 0;
  14.     while(n > 0) {
  15.         int dig = n % 2;
  16.         if(dig == 1) {
  17.              cont++;
  18.         }
  19.         n /= 2;
  20.     }
  21.     return cont;
  22. }
  23.  
  24. int countPrimeSetBits(int L, int R) {
  25.         // Write your code here
  26.     int primos[] ={ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
  27.     int size = sizeof(primos) / sizeof(int);                        
  28.    
  29.     set<int> hash;
  30.     for(int i =0; i<size; i++) {
  31.         hash.insert(primos[i]);
  32.     }
  33.    
  34.  
  35.     int cont = 0;
  36.     for(int i = L; i<= R; i++) {
  37.         int unos = NumUnos(i);
  38.         if( hash.find(unos) != hash.end()) {
  39.             cont ++;
  40.         }
  41.     }
  42.    
  43.     return cont;
  44. }
  45.  
  46. // driver  program to test above function
  47. int main()
  48. {
  49.     /*
  50.     int N = 13 ; // Must be less than MAX_SIZE
  51.  
  52.     manipulated_seive(N);
  53.  
  54.     // pint all prime number less then N
  55.     for (int i=0; i<prime.size() && prime[i] <= N ; i++)
  56.         cout << prime[i] << " ";
  57.     */
  58.     //int  L = 6, R = 10;
  59.     int L = 10, R = 15;
  60.    
  61.     //int L =
  62.     cout << countPrimeSetBits( L,  R) << endl;
  63.    
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement