Pernicious numbers | Rosetta Code | #15

URL to the problem page: http://rosettacode.org/wiki/Pernicious_numbers


A pernicious number is a positive integer whose population count is a prime.
The population count is the number of ones in the binary representation of a non-negative integer.

Example

22 (which is 10110 in binary) has a population count of 3, which is prime, and therefore 22 is a pernicious number.


Display the first 25 pernicious numbers (in decimal).
Display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive).



#include <iostream>
using namespace std;

int main() {
    int cnt = 0, cnt2, cnt3, tmp, binary[8];

    cout << "First 25 pernicious numbers are:" << endl;
    for (int i = 3; cnt < 25; i++) {
        tmp = i;
        cnt2 = 0;
        cnt3 = 0;
        for (int j = 7; j > 0; j--) {
            binary[j] = tmp % 2;
            tmp /= 2;
        }
        binary[0] = tmp;
        for (int j = 0; j < 8; j++) {
            if (binary[j] == 1) {
                cnt2++;
            }
        }
        for (int j = 2; j <= (cnt2 / 2); j++) {
            if (cnt2 % j == 0) {
                cnt3++;
                break;
            }
        }
        if (cnt3 == 0 && cnt2 != 1) {
            cout << i << ", ";
            cnt++;
        }
    }

    cout << endl << endl << "All pernicious numbers between 888,888,877 and 888,888,888 (inclusive) are:" << endl;;
    int binary2[31];

    for (int i = 888888877; i <= 888888888; i++) {
        tmp = i;
        cnt2 = 0;
        cnt3 = 0;
        for (int j = 30; j > 0; j--) {
            binary2[j] = tmp % 2;
            tmp /= 2;
        }
        binary2[0] = tmp;
        for (int j = 0; j < 31; j++) {
            if (binary2[j] == 1) {
                cnt2++;
            }
        }
        for (int j = 2; j <= (cnt2 / 2); j++) {
            if (cnt2 % j == 0) {
                cnt3++;
                break;
            }
        }
        if (cnt3 == 0 && cnt2 != 1) {
            cout << i << endl;
        }
    }
    return 0;
}



Comments

My photo
Ercan Tomac
instagram.com/ercantomac