Question

In: Computer Science

The greatest common divisor of two integers x and y is the largest positive integer that...

The greatest common divisor of two integers x and y is the largest positive integer that evenly divides both. For example, gcd(12,6)=6, gcd(21,14)=7, gcd(31,10)=1, gcd(55,25)=5

Recall that the gcd of two integers is gcd(a,0) = a gcd(a,b) = gcd(b, a%b) Create int gcd(int x, int y).

Assume that the two integers are zero or positive. Write the code as a pure function. Put it in a file gcd.c. Of course, you will need to test it.

The function Φ(n) counts how many integers 1 to n are relatively prime to n. Two integers x and y are relatively prime when gcd(x,y)= 1. So 5 and 12 are relatively prime, but 3 and 12 are not.

For example, Φ(1)=1, Φ(2)=1, Φ(5)=4, Φ(6)=3, Φ(8)=4, Φ(31)=30 Write a function int phi(int n) that computes this. Make this a pure function. Put it in its own file, phi.c. Write a driver that contains main() to test it. Put this in its own file phiTester.c.

C:\Source>gcc phiTester.c gcd.c phi.c

C:\Source>.\a.exe

Enter n: 259

Phi is 216

Turn in separate source files tau.c, tauSearch.c, phi.c and phiTester.c.

From poster: Please use comments, I want to see how it works!

Solutions

Expert Solution

// gcd.c

// function that calculates the gcd of x and y where x, y >=0
int gcd(int x, int y)
{

   int a, b;
   // check if both x and y are zero then return 0
   if(x != 0 && y!=0)
   {
       // determine which is larger
       // store the larger is a and smaller in b
       if(x > y)
       {
           a = x;
           b= y;
       }
       else{
           a= y;
           b=x;
       }

       int r=a%b; // calculate the remainder left when a is divided by b
       // loop that continues till we get b which divides a completely
       while(r!=0)
       {
           a=b; // assign b to a
           b=r; // assign the remainder to b
           r=a%b; // calculate the remainder left when a is divided by b
       }
   }else
       b=0;

   // gcd of x and y is stored in b
   return b;
}
//end of gcd.c

// phi.c

// function prototype
int gcd(int x, int y);

// function to count the number of integers from 1 to n that are relatively prime to n
int phi(int n)
{
   int count = 0, i;
   // loop that continues from 1 to n-1
   for(i=1;i<n;i++)
   {
       // if gcd of i and n = 1, increment count
       if(gcd(i,n) == 1)
           count++;
   }

   return count; // return the count
}
//end of phi.c


// phiTester.c : C program to input n and calculate the count of integers from 1 to n that are relatively prime to n
#include <stdio.h>
#include <stdlib.h>

// function prototype
int phi(int n);

int main()
{
   int n;
   // input of n
   printf("Enter n: ");
   scanf("%d",&n);
   // display the count of integers that are relatively prime to n
   printf("Phi is %d",phi(n));
   return 0;
}
//end of phiTester.c

Output:


Related Solutions

In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and uses your function to...
Let a and b be positive integers, and let d be their greatest common divisor. Prove...
Let a and b be positive integers, and let d be their greatest common divisor. Prove that there are infinitely many integers x and y such that ax+by = d. Next, given one particular solution x0 and y0 of this equation, show how to find all the solutions.
Write a C function gcd that returns the greatest common divisor of two integers. The greatest...
Write a C function gcd that returns the greatest common divisor of two integers. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers.
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y):     5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, write gcd(xy). Write a recursive function, gcd, that takes two integers as parameters and returns the greatest common divisor of numbers. Also write a program to test your function. Write a recursive function, reverseDigits, that takes an integer as a parameter and returns the number with the digits reversed. Also write a program to test your application.
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its...
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its coefficients is 1. Prove the product of two primitive polynomials is primitive. [Hint: Use proof by contradiction.]
Thank You Define the gcd of three integers a, b, c as the largest common divisor...
Thank You Define the gcd of three integers a, b, c as the largest common divisor of a, b, c, and denote it by (a, b, c). Show that (a, b, c) = ((a, b), c) and that (a, b, c) can be expressed as a linear combination of a, b, c.
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf)...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers. The greatest common divisor of a and b is written as gcd (a, b), or sometimes simply as (a, b). For example, gcd (12, 18) = 6, gcd (−4, 14) = 2 and gcd (5, 0) = 5. Two numbers are called co-prime or relatively prime...
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use...
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55). Activity 7. Write a Prolog program to find the last item in a list. give me the screenshot pls
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT