Skip to the content of the web site.

Project H.1.a: Sieve of Eratosthenes

Write a function with the declaration

void print_primes( unsigned long n );

which prints all prime numbers up to and possibly including $n$ separated by a comma followed by a space and the last number should be followed by an end-of-line. For example, the output of the call print_primes( 70 ) should be

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67

Remember that you can declare a local array with a capacity specified by a parameter:

void print_primes( unsigned long n ) {
	bool numbers[n + 1];
	// Other code here...
}

You can test your code with

#include <iostream>

// Function declarations
void print_primes( unsigned long n );
int main();

// Function definitions
int main() {
	for ( unsigned long k{10}; k <= 80; k += 10 ) {
		print_primes( k );
	}

	return 0;
}

// Your code here;

The output should be

2, 3, 5, 7
2, 3, 5, 7, 11, 13, 17, 19
2, 3, 5, 7, 11, 13, 17, 19, 23, 29
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79