(1) (10 points) Prime numbers can be generated by an algorithm known as the Siev
ID: 3731026 • Letter: #
Question
(1) (10 points) Prime numbers can be generated by an algorithm known as the Sieve of Eratosthenes. The algorithm for this procedure is presented here. Write a program that implements this algorithm. Have the program find and display all prime numbers up to n = 150. Sieve of Eratosthenes Algorithm To Display All Prime Numbers Between 1 and n Step 1: Define an array of integers P. Set all elements Pito 0, 2 n, the algorithm terminates. Step 4: If P, is 0, then i is prime. Step 5: For all positive integer values of j, such that i *j s n, set Pyto Step 6: Add 1 to i and go to step 3.Explanation / Answer
/* Prime Number Program using Sieve of Eratosthenes Algorithm between 1 and n */
#include<stdio.h>
#include<stdbool.h>
int main()
{
int p, i, prime[150], prime_Index = 2;
bool isPrime;
prime[0] = 2;
prime[1] = 3;
for(p = 5; p <= 150; p += 2) {
isPrime = true;
for(i = 1; isPrime && p / prime[i] >= prime[i]; i++)
if(p % prime[i] == 0)
isPrime = false;
if(isPrime == true) {
prime[prime_Index] = p;
prime_Index++;
}
}
for( i = 0; i < prime_Index; i++)
printf("%i ", prime[i]);
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.