Question

Prime Number Programing in C Note: The program is to be written using the bitwise operation....

Prime Number Programing in C

Note: The 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.

I need to rewrite this program as stated below. The program needs to be written as follows: 1. USE an integer array (ex: int mask[NUM] - NOT A BOOLEAN ARRAY(not bool mask[NUM]. 2. USE a bitwise operation for the length of the bit. (ex. bitLength 32) to specify how the number would be stored in memory.

Not like the initial version of what I have written below:

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

int main ()

{

            int NUM = 10000;

            bool mask[NUM];

            int count=0;

            int lineCount=0;

            int numberOfComparison = 0;

            int numberOfAssignment = 0;

            int totalComputation = 0;

            //Initially masking all numbers as prime.

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

                        mask[i] = true;

            //This loop mask non-prime number.

            for(int p = 2; p <=NUM; p++)

            {

                        if(mask[p])

                        {

                                    numberOfComparison++;

                                    for(int j=p*2; j<=NUM; j=j+p)

                                                {mask[j] = false;

                                                numberOfAssignment++;

                                                }

                        }

            }

           

            //Print prime numbers.

            printf("\n First 500 Prime Numbers: \n");

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

                        {

                                    if(mask[i])

                                    {

                                                if (count < 500)

                                                            printf("%7d", i);

                                                count++;

                                                lineCount++;

                                    }

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

                                    {

                                                printf("\n");

                                                lineCount=0;

                                    }

                        }

           

            //Prints program results:

            printf("\n\n

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

            printf("\n comparison are: %d \n", numberOfComparison);

            printf("\n Assignments are: %d \n\n", numberOfAssignment);

           

            return (0);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* *Program name: primeNumber.c

* This is a C program that finds all prime numbers less than 10,000 using Sieve of Eratosthenes algorithm.

* The first five hundred prime numbers that are found are displayed using decimal (base 10) numbers.

*/

#include <stdio.h>

#include <stdlib.h>

int main()

{

int NUM = 10000;

int mask[NUM];

int count = 0;

int lineCount = 0;

int numberOfComparison = 0;

int numberOfAssignment = 0;

int totalComputation = 0;

//Initially masking all numbers as prime.

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

mask[i] = 1;

//This loop mask non-prime number.

for (int p = 2; p <= NUM; p++)

{

if (mask[p])

{

numberOfComparison++;

for (int j = p * 2; j <= NUM; j = j + p)

{

mask[j] = 0;

numberOfAssignment++;

}

}

}

printf("\n Hello dear user. This program computes prime numbers less than 10,000. "

"\n But only the first five hundred prime numbers are printed for your convenient.\n");

//Print prime numbers.

printf("\n First 500 Prime Numbers: \n");

for (int i = 2; i < NUM && count < 500; i++)

{

if (mask[i])

{

printf("%7d", i);

count++;

if (count % 10 == 0)

printf("\n");

}

}

totalComputation = numberOfComparison + numberOfAssignment;

//Prints program results:

printf("\n\n ********************************************************************** \n **********************************************************************\n");

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

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

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

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

return (0);

}

I have one doubt, why and where we need to use bitwise operations? I will definitely give solution, I am in little bit confuction...

Add a comment
Know the answer?
Add Answer to:
Prime Number Programing in C Note: The program is to be written using the bitwise operation....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Write an MPI program, countprimes which will count the number of prime numbers in the numbers...

    Write an MPI program, countprimes which will count the number of prime numbers in the numbers from 1 to n inclusive where n is a long integer. The value for n which can be set in the program using a constant should be 50,000. Each process will test its share of the cases. Each process should print out any primes that it finds in a readable manner indicating which process found it and the actual prime. The master process should...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment wil...

    CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment will be dropped. Important: This is an individual assignment. Please do not collaborate. It must be submitted on-line (Blackboard). No late assignment will be accepted Minimal Submitted Files You are required to turn in the following source file: assignment11.s Objectives: -write assembly language programs to:             -perform arithmetic on floating point numbers             -use syscall operations to display floating point numbers and strings on the console window             -use syscall...

  • any help! my program not working , there is an error but cannot solve it i...

    any help! my program not working , there is an error but cannot solve it i did this program after editing a previous program  and i tried to follow the following ; 1. Instead of printing the prime numbers from 2 to n, your program will print the first n prime numbers. 2. It will be an error if n is less than 1. Exampl: $ ./a 1 2 3 #include<stdio.h> 4 int p; // p is the global variable. 5...

  • Write a C program that uses the bitwise shift operators to shift the bits to the...

    Write a C program that uses the bitwise shift operators to shift the bits to the right >> or the left > m; /* This shifts m bits to the right, and the m least significant bits are lost.*/ The following statements are the same. num = num >> 3; num >>= 3; Show the operation in binary by calling the following function as defined in 3.1, void to_binary(unsigned int n); The function converts decimal to binary and outputs the...

  • Help with a Parallel and Distributed Programming assignment. In this assignment, you will be expl...

    Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...

  • /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /*...

    /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /* and zero crossings from a speech signal. */ #include <stdio.h> #include <math.h> #define MAXIMUM 50 int main(void) { /* Declare variables */ int k=0, npts=30; double speech[MAXIMUM]={0.000000,-0.023438,-0.031250,-0.031250,-0.039063,-0.039063,-0.023438,0.000000,0.023438,0.070313,-0.039063,-0.039063,0.046875,0.101563,0.117188,0.101563,0.070313,0.054688,0.023438,0.000000,-0.031250,-0.039063,-0.070313,-0.070313,-0.070313,-0.070313,-0.062500,-0.046875,-0.039063,-0.031250}; /* Declare the function prototypes */    /* Compute and print statistics. */ printf("\n SPEECH DATA "); print(,,);//complete the statement printf("\n"); printf(" SPEECH STATISTICS : \n"); printf(" average power: %f \n",);//complete the statement printf(" average magnitude: %f...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT