Massimo dell'efficienza(beh, diciamo che partendo dal crivello ho fatto quel che ho potuto):
Codice: Seleziona tutto
#include "stdafx.h"
#include <iostream>
#include <math>
using namespace std;
union bools{
public:
unsigned char byte;
struct bits{
public:
unsigned char _0:1, _1:1, _2:1, _3:1, _4:1, _5:1, _6:1, _7:1;
void set(unsigned int n){
switch(n){
case 0:_0=0;return;
case 1:_1=0;return;
case 2:_2=0;return;
case 3:_3=0;return;
case 4:_4=0;return;
case 5:_5=0;return;
case 6:_6=0;return;
} _7=0;
}
unsigned char get(unsigned int n){
switch(n){
case 0:return _0;
case 1:return _1;
case 2:return _2;
case 3:return _3;
case 4:return _4;
case 5:return _5;
case 6:return _6;
} return _7;
}
} bit;
};
int main(){
#define MAX 4294967280 //il massimo che il computer regge usando interi a 32bit
#define crivella() for(j= i8*(t=2*i8+2), t--; j<HMx; j+=t){jo8 = j/8; jm8 = j%8; seek(jo8, jm8)}
#define seek(a, n) switch(n){case 0:V[a].bit._0=0;break;case 1:V[a].bit._1=0;break;case 2:V[a].bit._2=0;break;case 3:V[a].bit._3=0;break;case 4:V[a].bit._4=0;break;case 5:V[a].bit._5=0;break;case 6:V[a].bit._6=0;break;default:V[a].bit._7=0;}
unsigned int t, i, j, i8, jo8, jm8,
Max = (MAX+15)/16,
rMax = ((unsigned int)sqrt((long double)MAX) + 15)/16,
HMx = (MAX+1)/2;
bools* V = new bools[Max];
for(i=0; i<Max; i++)
V[i].byte = 0xff;
V[0].bit._0 = 0;
for(i=0; i<rMax; i++){
i8 = i*8;
if(V[i].bit._0)
crivella();
i8++;
if(V[i].bit._1)
crivella();
i8++;
if(V[i].bit._2)
crivella();
i8++;
if(V[i].bit._3)
crivella();
i8++;
if(V[i].bit._4)
crivella();
i8++;
if(V[i].bit._5)
crivella();
i8++;
if(V[i].bit._6)
crivella();
i8++;
if(V[i].bit._7)
crivella();
}
//FILE *fw;
//fopen_s(&fw, "output.txt", "w");
//fprintf(fw, "%u\n", 2);
//for(i=1; i<HMx; i++)
// if(V[i/8].bit.get(i%8))
// fprintf(fw,"%u\n",2*i+1);
//cout << 2 << endl;
//for(i=j=1; i<HMx; i++){
// if(V[i/8].bit.get(i%8)){
// cout << 2*i+1;
// if(++j%20)
// cout << endl;
// else{
// char pause[2];
// cin.getline(pause, 1);
// }
// }
//}
cout << ">- Finito -<" << endl;
char pause[2];
cin.getline(pause, 1);
return 0;
}
tempo di uso cpu: 1'8"
@Reese: const e define sono viversi. define è più veloce perchè viene sostituito in fase di compilazione, mentre const è una normale variabile, solo non modificabile, che quindi è accessibile a run-time.
@SkZ: Non mi risultano linguaggi di programmazione che usino un bit per la memoria, infatti la ram si è ridotta ad un'ottavo.