Cod sursa(job #2335981)

Utilizator AxellApostolescu Alexandru Axell Data 4 februarie 2019 18:10:19
Problema Ciurul lui Eratosthenes Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char *set;

// Se apeleaza cu numere impare
void remove_from_set(set s, int val) {
	if (!(val & 1)) {
		return;
	}
	int byte = (val >> 4), bit = (val >> 1) % 8;
	unsigned char mask = 1 << bit;
	s[byte] |= mask;
}

// Intoarce 1 pentru adevarat
int is_in_set(set s, int val) {
	int byte = (val >> 4), bit = (val >> 1) % 8;
	unsigned char mask = 1 << bit;
	if ((s[byte] & mask)) {
		return 0;
	}
	return 1;
}

void ciur(set s, int n, FILE* out) {
	int ct = 1, i, j;
	// Ct incepe de la 1 deoarece se numara si 2
 	for (i = 1 ; 2 * i + 1  <= n ; ++i) {
		if (is_in_set(s, 2 * i + 1)) {
			ct++;
			for (j = 2 * (2 * i + 1) ; j <= n ; j += 2 * i + 1) {
				remove_from_set(s, j);
			}
		}
	}
	fprintf(out, "%d\n", ct);
}

int main() {
	FILE *in, *out;
	if (((in = fopen("ciur.in", "rt")) == NULL)) {
		printf("Nu am putut deschide fisierul de input!");
		return -1;
	}
	if (((out = fopen("ciur.out", "wt")) == NULL)) {
		printf("Nu am putut deschide fisierul de output!");
		return -2;
	}
	int n;
	fscanf(in, "%d", &n);
	set s = calloc((n + 15) / 16, sizeof(char));
	if (s == NULL) {
		printf("Nu am putut aloca memorie\n");
		return -3;
	}
	ciur(s, n, out);
	// Freeing the memory
	free(s);
	fclose(in);
	fclose(out);
	return 0;
}