Cod sursa(job #557927)

Utilizator Rares95Rares Arnautu Rares95 Data 16 martie 2011 23:05:12
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
# include <fstream>
  using namespace std;
	  
	  int n;
		char w [ ( 10000000 >> 3 ) + 1 ];
		
		int ciur ()
		{ int i, j, x, nr = 1;
		  for ( i = 4; i <= n; i += 2 )
				w [ i >> 3 ] |= ( 1 << ( i & 7 ) );
			for ( i = 3; i <= n; i += 2 )
				if ( !(w [ i >> 3 ] & ( 1 << ( i & 7 ) ) ) )
				{ ++nr;
				  for ( j = i + i + i, x = i << 1; j <= n; j += x )
						w [ j >> 3 ] |= ( 1 << ( j & 7 ) );
				}
			return nr;
		}
		
		int main ()
		{ ifstream f ( "ciur.in" );
		  ofstream g ( "ciur.out");
			
			f >> n;
			
			g << ciur() << '\n';
			
			g.close ();
			return 0;
		}