Finding the Greatest Common Divisor and Relatively Prime Numbers, Exams of Cryptography and System Security

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-ck7-1
koofers-user-ck7-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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=qb+rwith 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:
63=1*49+14
49=3*14+7
14 = 2*7
Þ
ÞÞ
Þ
(63,49) = 7
96=1*57+39
57=1*39+18
39=2*18+3
18 = 6*3
Þ
ÞÞ
Þ
(96,57) = 3
55=2*27+1
27 = 27*1
Þ
ÞÞ
Þ
(55,27) = 1
1. Complete the following code for a method for finding the gcd of two
non-zero integers
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
}
pf3
pf4
pf5

Partial preview of the text

Download Finding the Greatest Common Divisor and Relatively Prime Numbers and more Exams Cryptography and System Security in PDF only on Docsity!

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:

ÞÞÞÞ

ÞÞÞÞ

ÞÞÞÞ

  1. Complete the following code for a method for finding the gcd of two non-zero integers

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 }

  1. Two integers whose gcd is 1 are said to be relatively prime. For example 1 and 2 are relatively prime 1 and 3 are relatively prime. The integers that are between 1 and 15 and relatively prime to 15 are

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

  1. Suppose a PrintOut(int N) routine is added to the class RelativelyPrime. Thus, in outline, the final program looks like this:

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

  1. Write a new version of the application TestRelPrime that, using the class of problem 4, will calculate the number of rational fractions a/b where

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