



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of the greatest common divisor (gcd) of two non-zero integers and provides an algorithm for determining it. It also introduces relatively prime numbers and explains how to find all the integers between 1 and a given number that are relatively prime to that number. Code snippets for implementing the gcd method and a class for finding relatively prime numbers.
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Sample Hour Exam
Background to Problem 1: The greatest common divisor (gcd) of two non- zero integers a and b is the largest integer d that that divides both a and b. If d is the gcd of a and b we use the notation (a, b) = d.
For example, the gcd of 15 and 25 is 5; (25,15) = 5. The gcd of 49 and 63 is 7; (63,49) = 7.
There is an algorithm for determining the gcd od a and b that is based
on repeated applications that of idea that if a ≥≥≥≥ b > 0 then
a = q b + r with 0 ≤≤≤≤ r < b.
In words, q the quotient and r is the remainder you get when you divide a by b.
This algorithm for the three cases (63,49), (96,57) and (55,27) works as follows:
public static int gcd(int a, int b) { int A = Math.abs(a); int B = Math.abs(b);
int c = Math.max(A,B); int d = Math.min(A,B); int r = c%d;
while( r != 0) { // <- Enter the appropriate code here
return ; // <- And here }
Suppose you have the gcd() method of problem 1 written and working. Complete the following code for a class for determining all the
integers that are ≥≥≥≥ 1 and < n and relatively prime to N:
public class RelativelyPrime { int total; int numer[] = new int[total];
public RelativelyPrime(int N) { int count = 0; // count the numbers relatively prime to N below:
// now save them in array total = count; numer = new int[total]; count = 0;
}// end RelativelyPrime(int N)
public static int gcd(int a, int b) { //From problem 1 // You don't have to fill this in // But the gcd() method will be used above } }// end class RelativelyPrime
public class RelativelyPrime { int total; int numer[] = new int[total];
public RelativelyPrime(int N) { // From Problem 2 // You don't have to fill this in
}// end RelativelyPrime(int N)
public static int gcd(int a, int b) { //From problem 1 // You don't have to fill this in }
public void PrintOut(int N) { // This prints out the integers between 1 and N // that are relatively prime to N // You don't have to fill this in }
}// end class RelativelyPrime
Suppose that the details to this have been added, and the class has been debuuged, so is working.
Write a new version of the application TestRelPrime, that uses the above class and will print out the integers between 1 and 99 that are relatively prime to 99:
public class TestRelPrime { public class void main(String args[]) {
}// end //
}// end class TestRelPrime
0 < a/b < 1, a and b are relatively prime, and 2 ≤≤≤≤ b < 999
public class TestRelPrime { public class void main(String args[]) {
}// end main(String args[])
}// end class TestRelPrime