diff options
Diffstat (limited to 'rushs/evalexpr/sieve_eratosthenes_advanced')
| -rw-r--r-- | rushs/evalexpr/sieve_eratosthenes_advanced/Makefile | 13 | ||||
| -rw-r--r-- | rushs/evalexpr/sieve_eratosthenes_advanced/sieve.c | 39 |
2 files changed, 52 insertions, 0 deletions
diff --git a/rushs/evalexpr/sieve_eratosthenes_advanced/Makefile b/rushs/evalexpr/sieve_eratosthenes_advanced/Makefile new file mode 100644 index 0000000..c7e35f9 --- /dev/null +++ b/rushs/evalexpr/sieve_eratosthenes_advanced/Makefile @@ -0,0 +1,13 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -Werror -Wvla -pedantic +LDLIBS= + +all: sieve.o + +sieve.o: sieve.c + $(CC) $(CFLAGS) -c -o sieve.o sieve.c + +.PHONY: clean + +clean: + rm sieve.o diff --git a/rushs/evalexpr/sieve_eratosthenes_advanced/sieve.c b/rushs/evalexpr/sieve_eratosthenes_advanced/sieve.c new file mode 100644 index 0000000..7dd4816 --- /dev/null +++ b/rushs/evalexpr/sieve_eratosthenes_advanced/sieve.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdlib.h> + +void sieve(int n) +{ + if (n <= 2) + { + return; + } + + // Generate array + int *a = calloc(n, sizeof(int)); + int count = 0; + + // Actual sieve and count + for (int i = 2; i < n; i++) + { + if (a[i] == 0) + { + for (int k = 2 * i; k < n; k += i) + { + a[k] = 1; + } + } + } + + for (int i = 2; i < n; i++) + { + if (a[i] == 0) + { + count++; + } + } + + // Print the count + printf("%d\n", count); + + free(a); +} |
