Cod sursa(job #557809)

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