Cod sursa(job #2781470)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 9 octombrie 2021 17:09:09
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <stdio.h>
unsigned char ciur[ ( 1 << 17 ) ];
 
int main()
{
    int n;
 
    FILE *fin = fopen( "ciur.in", "r" );
    fscanf( fin, "%d", &n );
    fclose( fin );
 
    int no = 1, i;
    for( i = 3; i * i <= n; i += 2 )
        if( !( ciur[ i >> 4 ] & ( 1 << ( ( i >> 1 ) & 7 ) ) ) ) {
            ++no;
            for( int j = i * i; j <= n; j += i + i )
                ciur[ j >> 4 ] |= 1 << ( ( j >> 1 ) & 7 );
        }

    for( ; i <= n; i += 2 )
        if( !( ciur[ i >> 4 ] & ( 1 << ( ( i >> 1 ) & 7 ) ) ) )
            ++no;
 
    FILE *fout = fopen( "ciur.out", "w" );
    fprintf( fout, "%d\n", no );
    fclose( fout );
    return 0;
}