Question: C Code - Repetition Statements. Only allowed to use my skeleton code and nothing else! Skeleton Code: // countCoprimes.c // To count the number of

C Code - Repetition Statements. Only allowed to use my skeleton code and nothing else!

C Code - Repetition Statements. Only allowed to use my skeleton code

and nothing else! Skeleton Code: // countCoprimes.c // To count the number

Skeleton Code:

// countCoprimes.c // To count the number of pairs of integers in the range [2, limit] // which are coprime. #include  int count_coprimes(int); int gcd(int, int); int main(void) { int limit; printf("Enter limit: "); scanf("%d", &limit); printf("Answer = %d ", count_coprimes(limit)); return 0; } // Return the number of pairs of integers in the range [2, lim] // which are coprime. // Precond: lim > 2 int count_coprimes(int lim) { return 1; // this is just a dummy return value } // Return the GCD of a and b // This is a badly written gcd() function. We will discuss this // function and write a better one in Week 6 discussion session. // Precond: a>=0, b>=0 and not both = 0 int gcd(int a, int b) { int divisor; if (a == 0) return b; else if (b == 0) return a; if (a   Practice S03P06: Count pairs of coprime numbers http://www.comp.nus.edu.sg/~cs1010/4 misc/practice.html Week of release: Week 4 Objective: Repetition statement Task statement: Write a program countCoprimes.c to read in a positive integer larger than 2, and smaller than or equal to 1000. Let's call this limit. There is no need for you to do input data validation. Your program is to determine the number of pairs of integers in the range [2, limit] which are coprime. Two positive integers a and b are said to be coprime (or relatively prime) if the only positive integer that divides both a and b is 1. Hence, 4 and 9 are coprime, but 24 and 15 are not. For example, if limit is 7, then there are 11 pairs of coprime integers: (2, 3), (2, 5), (2, 7), (3, 4), (3, 5), (3, 7), (4, 5), (4, 7), (5, 6), (5, 7) and (6, 7). Note that the pairs (2, 3) and (3, 2) are considered the same, so they are counted as one pair. Your program should contain a function count_coprimes (int limit) that takes in limit and computes the number of pairs of integers in the range [2, limit] that are coprime. The skeleton program provided contains a function gcd() that computes the Greatest Common Divisor of two integers. This function works, but is badly designed (and runs very slowly!). In Week 6 discussion session, we will discuss this and you will be shown a better version. Because of the bad gcd() function given, your program will be tested with inputs not more than 1000

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!