Question: COULD SOMEONE ALSO HELP WITH THE MAKE FILE I have a code below but I have to modify it to add alloc and realloc to

COULD SOMEONE ALSO HELP WITH THE MAKE FILE

I have a code below but I have to modify it to add alloc and realloc to reallocate the size of the array can someone please help. Look at my code below please

And i need a make follow to foloow the instructions below please help me!

Can someone honestly help me with this i have to program this in C and the assignements there and honestly its just so hard

I have to find a prime number and store all the prime numbers up to that number including that number so when I check a bigger number it will start right off that

last number like if i checked 1,000,000 then 1,000,234 I wouldnt need to check all of them I would just have to check to the point after 1,000,000 because

I already checked it and found all the prime up to that number

To check whether a given non-negative integer n is a prime, it suffices to check if it is divisible by at least one of the elements in Sn = {ki P|ki <

n}. If Sn is known, it takes |Sn | operations to check for primality of

n. This is much better than when Sn is not known in which case it takes

O(n log log n) operations to construct it.

Suppose we can store P = Sn , as set of all known prime numbers, in a file and access it when testing primality of another non-negative integer m. This way, if 1 << m < n, primality testing of m would be trivial. On the other hand, if 1 << n < m, Sn would still be very useful in constructing Sm and required operations for prime-checking m will be reduced by c1 n log log n + c2 operations where c1 and c2 are positive integers. In this case, once m is prime-checked, we can update the list of known prime numbers to Sm for later use.

You are asked to write a program fast-prime.c that accepts a set of non- negative integer numbers as command line arguments and, using the above- mentioned algorithm, checks for their primality.

You may not include any header file other than fast-prime.h. In addition, your fast-prime.c file should accompany a Makefile to facilitate building process and artifact removal.

Following illustrates the expected functionality of your program.

$ ls

fast-prime.c fast-prime.h Makefile

$ make

gcc -c -o fast-prime.o fast-prime.c

gcc -o fast-prime fast-prime.o -Werror -Wall -std=gnu99 -I.

$ ./fast-prime

error: missing command line argument

$ ./fast-prime 6

6 : not prime

$ ls

fast-prime fast-prime.c fast-prime.h fast-prime.o

Makefile primes.log

$ cat primes.log

2

3

5

$ ./fast-prime 25 37 39 29

25 : not prime

37 : prime

39 : not prime

9 : prime

$ ls

fast-prime fast-prime.c fast-prime.h fast-prime.o Makefile primes.log

$ cat primes.log

2

3

5

7

11

13

17

19

23

29

31

37

$ make clean

rm -rf fast-prime rm -rf primes.log

find . -name *.o -delete

#include #include FILE *fp; /*this function returns 1 (true) if its prime or 0 (false) if its not prime*/ int isPrime(int n){ int i;

if (n==2) return 1;

if (n%2==0) return 0; /* sqrt() function is used here to check until square root of that num */ for (i=3;i<=sqrt(n);i+=2) if (n%i==0) return 0;

return 1; }

int main(){ int i,n,a[10000]; int count = 0,op,num,x=0,k,l=0; fp=fopen("primes.txt","r"); /* Here we open some file (any file that has to be created) which maybe empty or has some already stored some prime numbers */ while( fscanf(fp, "%d,", &i )!=EOF)

{ a[l]=i; /* if theres any number in file then we store it in array */ l++; } if(l>1) { x=a[l-1]; /* here we store the last element of file in variable x */ printf("%d",x); /*this shows the previous largest integer value stored in file */ } fclose(fp); while(n!=1) { printf(" 1.Check for primes 2.Quit Enter your option: "); /* here user is asked to enter either 1 or 2 */ /* if 1 then user can enter any prime or user can enter 2 to quit */ scanf("%d",&op); if(op==1) { /* code reaches here if user enters 1 */ printf("Enter number: "); /* here user is asked to enter some number upto which he wants to know prime numbers */ scanf("%d",&num); /* Now here we are going to check if x=0 i.e. in above we have seen that if theres some large prime number already stored then if any number user enters which is greater than stored value we can neglect from primes and check from that prev value onwards*/ if(x==0) x=2; /* So here x can be previous number if theres any or we put 2 if theres no number(i.e. if x is zero) */ if(num>x) { /*Now here if theres already some value whose value is smaller than entered number then we can check in between that interval and also store the values*/ fp=fopen("primes.txt","a"); for (i=x;i<=num;i++) { if(isPrime(i)) { if(x!=i) { /* this condition x not equal to i makes sure that same value wont be printed in file again */ fprintf(fp," %d",i); /*All new primes will be printed in file*/ } printf("%d",i); printf(" "); k=i; /* this assignment statement stores upto last value so that we can use it as new stored prime value */ } } /* Now when the entered element is greater than previous stored value then we want that new large prime to be stored so that we can use it */ if(k>x) { x=k; } fclose(fp); } /* If entered number is less than previous stored value then we will print primes from starting 2 onwards but we wont enter these values in file.*/ else { for (i=2;i<=num;i++){ if(isPrime(i)) { printf(" %d",&i); } } } } else if(op==2) { n=1; break; } else { printf(" Enter right option."); } } return 0; }

Sample output:

1. Check for primes

2. Quit

Enter option : 1

Enter some num: 10

2 3 5 7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!