Todo app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1.5 KiB

title description navigation date img tags
Sieve of Eratosthenes Sieve of Eratosthenes. false 2021-03-02 02:31:16 /img/algorithms/sieve-of-eratosthenes.gif [Algorithm]

Implementation of the ancient sieve of Eratosthenes math algorithm, used for finding prime numbers up to a given limit.

sieve of eratosthenes

#include <stdbool.h> // bool
#include <stdio.h>   // printf
#include <string.h>  // memset

int main(int argc, char* argv[]) {
	int limit = 10000;
	int calculate = limit * 20;

	// Create an array and set all to true
	bool numbers[calculate];
	memset(&numbers, true, sizeof(bool) * calculate);

	// Loop through all the numbers
	for (int i = 2; i < calculate; i++) {

		// Already crossed out numbers don't have to be checked
		if (numbers[i]) {
			// Cross out all the numbers that can't be a prime, which are multiples of itself
			for (int j = i * i; j < calculate; j += i) {
				numbers[j] = false;
			}

			// Once you exceed the calculate range, you can exit the loop
			if (i * i > calculate) {
				break;
			}
		}
	}

	int sum = 0;
	int counter = 1;
	// Get the sum of the first 10000 primes
	for (int i = 2; i < calculate; i++) {
		if (numbers[i]) {
			sum += i;

			if (counter >= limit) {
				break;
			}
			counter++;
		}
	}

	printf("sum of first %d primes is: %d\n", counter, sum);

	return 0;
}

Source:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes{ target=_blank }