diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/sieve_eratosthenes_advanced | |
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/sieve_eratosthenes_advanced')
| -rw-r--r-- | rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile | 13 | ||||
| -rw-r--r-- | rushs/tinyprintf/sieve_eratosthenes_advanced/sieve.c | 39 |
2 files changed, 52 insertions, 0 deletions
diff --git a/rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile b/rushs/tinyprintf/sieve_eratosthenes_advanced/Makefile new file mode 100644 index 0000000..c7e35f9 --- /dev/null +++ b/rushs/tinyprintf/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/tinyprintf/sieve_eratosthenes_advanced/sieve.c b/rushs/tinyprintf/sieve_eratosthenes_advanced/sieve.c new file mode 100644 index 0000000..7dd4816 --- /dev/null +++ b/rushs/tinyprintf/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); +} |
