Cod sursa(job #2848603)

Utilizator om6gaLungu Adrian om6ga Data 12 februarie 2022 21:42:25
Problema Ciurul lui Eratosthenes Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <stdio.h>
//#include <stdlib.h>
#include <unistd.h>
#include <time.h>
 
#pragma GCC optimize("Ofast")
 
//#define is_set(x)  (~a[x>>3] && (a[x>>3] & r[x & 0x7]))
//#define is_set(x)  (a[x>>3] & r[x & 0x7])
#define is_set(x)  ( a[x>>3] & (1 << (x & 0x7)) )
#define set_bit(x) ( a[x>>3] |= (1 << (x & 0x7)) )
 
char r[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
int i, j, N, nr = 0, square_i, double_i;
char a[250001];
char tmp;

struct timespec start, stop;

int main()
{ 
	freopen( "ciur.in", "r", stdin );
	freopen( "ciur.out", "w", stdout);  
    scanf("%d",&N);
 
    //clock_gettime( CLOCK_REALTIME, &start);
    for (i = 3; (j = i*i) <= N; i += 2) {
        if (!is_set(i)) {
        	set_bit(j);
            nr++;
            j += (double_i = (i<<1));
            /*for (j = square_i + (double_i = (i<<1)); j <= N; j += double_i) {
                if (!is_set(j)) {
                	set_bit(j);
                	nr++;
				}
            }*/
            while (j <= N) {
            	nr += (!is_set(j) && set_bit(j));
            	j += double_i;
			}
        }                                    
    }
    /*clock_gettime( CLOCK_REALTIME, &stop);
    printf( "%d seconds %d nanoseconds\n", stop.tv_sec - start.tv_sec, stop.tv_nsec - start.tv_nsec);
    getchar();*/
 
    printf("%d\n", (N+1)/2 - nr);
    //fclose(in);
    //fclose(out);
    
    return 0;
}