generate functionSun, 25 Dec 2022

Write a prime number generator using the Sieve of Eratosthenes. Your program should require exactly one command-line argument, an integer (no smaller than 2) specifying the largest prime number to print. To find the prime numbers up to some maximum n, an array p[i] with indices 2 ≤ i ≤ n is used. Each element p[i] is true if i is prime and false if it is not. Initially every element in p is set to true. For each i between 2 and n, the element p[i] is tested. If p[i] is false then the number is not prime and can be ignored. If p[i] is true then then i is prime and all multiples of it p[n×i] (for n ≥ 1) in the array can be set to false, since they are multiples of a prime factor i. When the process completes, the elements p[i] that are true identify prime numbers i.

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX 100 int main(int argc, char *argv[]){ if(argc != 2){ printf("Must provide one command-line argument\n"); exit(1); } int n = atoi(argv[1]); if(n <= 1){ printf("Please give a number greater than 1\n"); exit(1); } bool p[MAX]; int i, j; for(i = 2; i < n; i++){ p[i] = true; } for(i = 2; i < n; i++){ if(p[i] == true){ for(j = i + i; j < n; j += i){ p[j] = false; } } } for(i = 2; i < n; i++){ if

Questions about programming?Chat with your personal AI assistant