Question

In: Computer Science

C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....

C Program only - MUST USE MALLOC IN CODE

Research and implement the Sieve of Eratosthenes.

Example Program Session (implement some linefeed ‘\n’ formatting):

Enter the limit: 1000
Primes up to 1000

   2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53
59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613
617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719
727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997

Number of primes: 168

Press any key to continue . . .

Solutions

Expert Solution

Code:

#include <stdio.h>
#include <stdlib.h>
int main(){
int i,j,LIMIT,count=0;
int *primes;
printf("Enter the limit: ");
scanf("%d",&LIMIT); //take limit from user
primes = malloc(sizeof(int)*LIMIT); //create array using malloc
//fill the array with 1
for (i=2;i<=LIMIT;i++)
primes[i]=1;
//sieve the non-primes and then put 0s on all the numbers that are not primes
for (i=2;i<=LIMIT;i++)
if (primes[i])
for (j=i;i*j<=LIMIT;j++)
primes[i*j]=0;
//print the numbers that are prime
printf("Primes up to %d\n",LIMIT);
for (i=2;i<=LIMIT;i++)
if (primes[i])
{
count++;
printf("%d\t",i);
}
printf("\nNumber of primes= %d",count);
return 0;
}

Output:


Related Solutions

PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient...
PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to √ n. The...
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
Code in C++ Learning Outcomes Implement two stacks and use them to implement an infix to...
Code in C++ Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the...
C++ code Output Formatting. Design and implement a complete C++ program that reads a customer record...
C++ code Output Formatting. Design and implement a complete C++ program that reads a customer record from the user such as the record has 4 fields (pieces of information) as shown below: Account Number (integer) Customer full name (string) Customer email (string) Account Balance (double) The program is expected to print the record in the format shown below: Account Number :  0000000 Name                     :  full name Email                      :  customer email Balance                  :  000000.00$ For example, if the user enters the following record 1201077 Jonathan I. Maletic [email protected]...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT