Pernicious numbers: Difference between revisions

(→‎{{header|Sidef}}: simpler and better code)
Line 422:
<lang cpp>
#include <iostream>
#include <algorithm>
#include <bitset>
 
using namespace std;
 
int main() {
class pernNumber
int cnt = 0, cnt2, cnt3, tmp, binary[8];
{
for (int i = 3; cnt < 25; i++) {
public:
tmp = i;
void displayFirst( unsigned cnt )
{ cnt2 = 0;
return cnt3 = 0;
unsigned pn = 3;
for (int j = 7; j > 0; j--) {
while( cnt )
binary[j] = tmp % 2;
{
tmp /= 2;
if( isPernNumber( pn ) )
{ }
binary[0] = tmp;
cout << pn << " "; cnt--;
for (int j = 0; j < 8; j++) {
}
if (binary[j] == 1) {
pn++;
cnt2++;
}
}
}
void displayFromTo( unsigned a, unsigned b )
for (int j = 2; j <= (cnt2 / 2); j++) {
{
if (cnt2 % j == 0) {
for( unsigned p = a; p <= b; p++ )
cnt3++;
if( isPernNumber( p ) )
break;
cout << p << " ";
}
{ }
if (cnt3 == 0 && cnt2 != 1) {
cout << pi << " "endl;
cnt++;
{ }
}
 
p.displayFirst( 25 ); cout << endl;
private:
int binary2[31];
bool isPernNumber( unsigned p )
 
{
for (int i = 888888877; i <= 888888888; i++) {
string bin = bitset<64>( p ).to_string();
tmp = i;
unsigned c = count( bin.begin(), bin.end(), '1' );
cnt2 = 0;
return isPrime( c );
} cnt3 = 0;
bool isPrime for (int unsignedj p= 30; j > 0; j--) {
binary2[j] = tmp % 2;
{
tmp /= 2;
if( p == 2 ) return true;
{ }
if( p < 2 || !( p % 2 ) ) return false;
for( unsigned x = 3; ( x * xbinary2[0] ) <= ptmp; x += 2 )
if( !( p % xfor )(int )j return= false0; j < 31; j++) {
if (binary2[j] == 1) {
return true;
cnt2++;
}
}
for (int j = 2; j <= (cnt2 / 2); j++) {
if (cnt2 % j == 0) {
cnt3++;
break;
}
}
if (cnt3 == 0 && cnt2 != 1) {
cout << i << endl;
}
}
};
int main( int argc, char* argv[] )
{
pernNumber p;
p.displayFirst( 25 ); cout << endl;
p.displayFromTo( 888888877, 888888888 ); cout << endl;
return 0;
}
</lang>