Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am working on the below prime number program and would appreciate some help. Please check the program instruction at the bottom of this

Hello, I am working on the below prime number program and would appreciate some help. Please check the program instruction at the bottom of this page(at the end of the code).

/* prime_number.c

*

*/

#include

#include

int main()

{

int primes[313];

int prime = 2, mask = 0, totalPrimes = 0, multiple = 0;

int max = 10000;

int count, lineCount, totalComputation, numberOfComparison, numberOfAssignment = 0;

for (int i = 0; i <= 312; i++)

{

mask = 1;

for (int j = 0; j <= 31; j++)

{

if ((primes[i] & mask) == 0)

totalPrimes ++;

{

for (multiple = prime * 2; multiple <= max; multiple += prime)

{

int primesIndex = multiple/32;

int bitIndex = multiple%32;

if (bitIndex ==0)

{

bitIndex = 32;

primesIndex -= 1;

}

mask = 2 ^(bitIndex -1);

primes[primesIndex] = (primes[primesIndex]|mask);

}

//if(count<500)

//printf("Testing Results: %d ", totalPrimes);

//if (totalPrimes%10 == 0);

//printf("Prime Numbers are: %d", primes);

//else printf("Prime again: ", prime);

//printf("%7d", prime);

}

mask *= 2;

prime++;

numberOfComparison ++;

printf("Testing Results: %d ", prime);

}

}

////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Print prime numbers.

printf(" First 500 Prime Numbers: ");

for(int i=2; i<313; i++)

{

if(primes[i])

{

if (count < 500)

printf(" %7d", i);

count++;

lineCount++;

}

if((lineCount > 0 && count < 500) && (lineCount %10==0))

{

printf(" ");

lineCount = 0;

}

}

/// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

totalComputation = numberOfComparison + numberOfAssignment;

//Prints program results:

//printf(" ");

printf(" The total prime numbers less than 10,000: %d ", count);

printf(" The number of comparison are: %d ", numberOfComparison);

printf(" The number of Assignments are: %d ", numberOfAssignment);

printf("===== Total number of computation =====: %d", totalComputation);

return (0);

}

/* Program instructions:

*C Prime Number

Note: This C program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits).

In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime, you should turn the corresponding bit off (on). Completion of the program requires going through all bits and printing the decimal numbers corresponding to those bits that remain on (off).

You should use the Sieve of Eratosthenes algorithm to find all the primes. The algorithm will be

discussed in class. You should investigate some efficiency steps to improve the performance of the algorithm to be measured in the number of comparisons and byte assignments that you use. For example, consider how you might make use of the fact that all even numbers after 2 will not be prime or how to reduce the number of comparisons and assignments when marking off multiples of higher primes.

After finding all primes, you should print the prime numbers out as decimal (base 10) numbers.

There may be a large number of prime numbers less than 10,000. To minimize the output, you should print the first 500 primes so that there are 5-10 prime numbers per line. You should then print a summary line of the total number of prime numbers that you found less than 10,000.

To provide a sense of how efficient your program is, you should output a count of the number of

comparisons and the number of assignments that your program has performed. This will be discussed further in class.

Be sure to submit an analysis of the results of your program. The analysis should include the number of comparisons and the number of assignments as a summary of the output from your program and some verification that the results are correct.

The program should be written to be as efficient as possible.

You should call printf with a decimal format (%d) to print a number. If you use a format of the form %7d as an example, it will print the number in a field width of 7. That is very convenient for printing the numbers in columns.

*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions

Question

Know the three main dimensions of the service environment.

Answered: 1 week ago