Question

The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int...

The following function is_prime() is not a very efficient prime number test:

#include <stdio.h>

int is_prime(int n)
{
int d;

for (d = 2; d < n; d++) {
if (!(n % d))
return 0;
}

return 1;
}


int main()
{
if (is_prime(7))
printf("Seven is Prime!\n");
  
return 0;
}

It is unnecessary to divide n by all numbers between 2 and n - 1 to determine if n is prime. Only divisors up to and including  need to be checked. Make this change.

Optimization: Computing a square root takes the CPU much longer than computing a square. So, instead of computing , compare d * d with n to determine when the last necessary divisor has been checked. Even though you will be squaring d every loop iteration, this will still be faster than computing the square root of n just once!

Next, replace the code in the main() function to use a for-loop with your improved implementation of is_prime() to compute and print the first 25 primes (starting with 2) to the screen.

A correctly written program should produce the following output:

1: 2

2: 3
3: 5

4: 7

5: 11

6: 13

7: 17

8: 19

9: 23

10: 29   

11: 31

12: 37

13: 41

14: 43

15: 47
16: 53
17: 59
18: 61
19: 67
20: 71
21: 73
22: 79
23: 83
24: 89
25: 97

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

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.

Thank You !
===========================================================================

#include <stdio.h>

int is_prime(int n)
{
int d;

for (d = 2; d*d <= n; d++) {
if (!(n % d)) return 0;
}

return 1;
}


int main()
{
       int num = 2;
       int count = 0;
       for(count=1; count<=25;){
           if(is_prime(num)){
               printf("%d: %d\n",count,num);
               count++;
           }
           num+=1;
       }
  
   return 0;
}

===================================================================

5 int d; { 7 8 9 for (d = 2; d*d <= n; d++) if (! (n & d)) return 0; ) C:\Users\User\Documents\is_prime.exe E 1: 2 2: 3 3: 5

Add a comment
Know the answer?
Add Answer to:
The following function is_prime() is not a very efficient prime number test: #include <stdio.h> int is_prime(int...
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
  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main()...

    Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main() { int a = 2, b = 7; b = f(&b, &a); printf("a = %d,\n", a); printf("b = %d\n", b); return 0; } int f(int* a, int* b) {     (*a) = (*a) + 3;     (*b) = 2*(*a) - (*b)+5;     printf("a = %d b = %d\n", *a, *b);     return(*a)+(*b); } can someone explain to me why the output is a =...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that...

    IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that is assumed to be greater than 1. The function will return a dictionary with keys the integers from 2 to n, inclusive, and with values the set of prime factors of each key. So, the command prime_factors.dict (8) will return the dictionary 2 123,3: 3),4 2),5 (53,6 2,3),7:7),8 {2)) Note that even though the prime number 2 appears twice in the prime fac- torization...

  • 1 #include<stdio.h> 53 int findMax (int, SA 5 int main() 6 { int n; int max;...

    1 #include<stdio.h> 53 int findMax (int, SA 5 int main() 6 { int n; int max; int maxLoc; co n = //ASSIGNMENT HERE 12 13 14 15 16 max = findMax (n, &maxLoc); n = n - max* (int) pow (10, maxLoc); //PRINT STATEMENT HERE 17 18 return(0); 19 ) 20 21 int findMax (int n, int *maxLoc) 22 { 23 int max = 0; int loc = 0; 24 25 26 while(n > 0) 27 28 if(n% 10 >...

  • In C Code provided: #include <stdio.h> int recursive_sequence(int n) { //type your code here } int...

    In C Code provided: #include <stdio.h> int recursive_sequence(int n) { //type your code here } int main() { int number; scanf("%d", &number); printf("recursive_sequence(%d) = %d",number, recursive_sequence(number)); return 0; } The nth term in a sequence of numbers called recursive_sequence can be defined as follows: • recursive_sequence(0) = 0 • recursive_sequence(1) = 1 If n is greater than 1, then recursive_sequence(n) = 2* recursive_sequence(n - 2) + recursive_sequence(n-1) The first 6 terms in this sequence are: 0,1, 1, 3, 5, 11...

  • 1) (5 points) The following program is used to display numbers between two intervals include stdio.h...

    1) (5 points) The following program is used to display numbers between two intervals include stdio.h define true 1 define false 0 5 void prime (int low, int high) int i -o, flag-0 ; printf ("Prime numbers between %d and %d are: ", low, high); while (low <high) 10 flag false; 12 13 for (i 0; i <-low/2; ++i) - 15 16 17 if(low % ?--0) flagtrue: break; 19 20 21 if (flagtrue ) 23 24 25 26 27 28...

  • what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n");...

    what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n"); for ( 2 { total = i + j; printf("%d", total); بہا } getchar(); return 0; } Find the for loop content | Output of progrm 5 15 1 11 21 31 41 51 61 2 12 22 32 25 3 13 23 33 43 53 63 4 14 24 34 44 54 64 35 45 6 16 26 36 46 56 66 7...

  • Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int...

    Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() {     int number;     scanf("%d", &number);     if (number % 2 == 0) {         printf("Even\n");     }     else {         printf("Odd\n");     }     return 0; }

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

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