0% found this document useful (0 votes)
24 views

5 Ext GCD

This document describes a graduate-level mathematics course titled "C++ for Mathematicians" taught by John Perry at the University of Southern Mississippi in Spring 2017. The document outlines the course, which covers implementing algorithms in C++, including passing arguments by reference, overloading functions, and implementing the extended Euclidean algorithm to find the greatest common divisor of two integers. An example calculation of the greatest common divisor of 26 and -14 is shown step-by-step.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

5 Ext GCD

This document describes a graduate-level mathematics course titled "C++ for Mathematicians" taught by John Perry at the University of Southern Mississippi in Spring 2017. The document outlines the course, which covers implementing algorithms in C++, including passing arguments by reference, overloading functions, and implementing the extended Euclidean algorithm to find the greatest common divisor of two integers. An example calculation of the greatest common divisor of 26 and -14 is shown step-by-step.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

MAT 685:

C++ for Math-


ematicians

John Perry

Background

Implementa-
tion
Passing by reference
MAT 685: C++ for Mathematicians
Overloading
Implementation The Extended Euclidean Algorithm
Summary

John Perry

University of Southern Mississippi

Spring 2017
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
Bézout’s Identity
John Perry

Background

Implementa-
tion Theorem (Bézout’s Identity)
Passing by reference
Overloading Let a, b ∈ Z. We can find x, y ∈ Z such that
Implementation

Summary
gcd (a, b) = ax + by .

Moreover, gcd (a, b) is the smallest positive integer of the form


ax + by where x, y ∈ Z.
MAT 685:
C++ for Math-
ematicians
Bézout’s Identity
John Perry

Background

Implementa-
tion Theorem (Bézout’s Identity)
Passing by reference
Overloading Let a, b ∈ Z. We can find x, y ∈ Z such that
Implementation

Summary
gcd (a, b) = ax + by .

Moreover, gcd (a, b) is the smallest positive integer of the form


ax + by where x, y ∈ Z.

Examples
• gcd (24, −16) = 8 = 24 × 1 + (−16) × 1
• gcd (24, −15) = 3 = 24 × 2 + × (−15) × 3
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation

Summary
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation
Modify recursive Euclidean Algorithm:
Summary given
• a, b ∈ Z arguments
• c = a mod b from recursion
• u, v ∈ Z s.t. gcd (b, c) = bu + cv from recursion
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation
Modify recursive Euclidean Algorithm:
Summary given
• a, b ∈ Z arguments
• c = a mod b from recursion
• u, v ∈ Z s.t. gcd (b, c) = bu + cv from recursion
choose q: a = qb + c compute
rewrite: c = a − qb theory
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation
Modify recursive Euclidean Algorithm:
Summary given
• a, b ∈ Z arguments
• c = a mod b from recursion
• u, v ∈ Z s.t. gcd (b, c) = bu + cv from recursion
choose q: a = qb + c compute
rewrite: c = a − qb theory
substitute: gcd (b, c) = bu + (a − qb)v theory
rewrite: gcd (b, c) = av + b (u − qv) theory
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation
Modify recursive Euclidean Algorithm:
Summary given
• a, b ∈ Z arguments
• c = a mod b from recursion
• u, v ∈ Z s.t. gcd (b, c) = bu + cv from recursion
choose q: a = qb + c compute
rewrite: c = a − qb theory
substitute: gcd (b, c) = bu + (a − qb)v theory
rewrite: gcd (b, c) = av + b (u − qv) theory
substitute: gcd (a, b) = av + b(u − qv) theory
MAT 685:
C++ for Math-
ematicians
Extended Euclidean Algorithm
John Perry

Background

Implementa-
Recall
tion If c = a mod b then gcd (a, b) = gcd (b, c).
Passing by reference
Overloading
Implementation
Modify recursive Euclidean Algorithm:
Summary given
• a, b ∈ Z arguments
• c = a mod b from recursion
• u, v ∈ Z s.t. gcd (b, c) = bu + cv from recursion
choose q: a = qb + c compute
rewrite: c = a − qb theory
substitute: gcd (b, c) = bu + (a − qb)v theory
rewrite: gcd (b, c) = av + b (u − qv) theory
substitute: gcd (a, b) = av + b(u − qv) theory
return gcd (a, b), x = v, y = u − qv compute
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation Compute gcd (26, −14):
Summary

gcd(26,-14) a=26, b=-14


• 26 ≥ 0 so no change to a
• −14 < 0 so b reassigned to 14
• a and b not 0
• c = 26 % 14 = 12
• return gcd(14,12)
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading Compute gcd (26, −14):
Implementation

Summary
gcd(26,-14)
gcd(14,12) a=14, b=12
• 14 ≥ 0 so no change to a
• 12 ≥ 0 so no change to b
• a and b not 0
• c = 14 % 12 = 2
• return gcd(12,2)
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Compute gcd (26, −14):
Implementation

Summary gcd(26,-14)
gcd(14,12)
gcd(12,2) a=12, b=2
• 12 ≥ 0 so no change to a
• 2 ≥ 0 so no change to b
• a and b not 0
• c = 12 % 2 = 0
• return gcd(2,0)
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Compute gcd (26, −14):
Overloading
Implementation
gcd(26,-14)
Summary
gcd(14,12)
gcd(12,2)
gcd(2,0) a=2, b=0
• 2 ≥ 0 so no change to a
• 0 ≥ 0 so no change to b
• but b = 0
∴ return a=2
. . . cascades back up: gcd (26, −14) = 2
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation
Compute gcd (26, −14):
Summary
gcd(26,-14)
gcd(14,12)
gcd(12,2)=2
gcd(2,0)=2
2=2×1+0×1
return 2, 1, 1
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Compute gcd (26, −14):
Overloading
Implementation

Summary
gcd(26,-14)
gcd(14,12)
gcd(12,2)=2
received 2, 1, 1
2=2×1+0×1
0 = 12 − 2 × 6
2 = 2 × 1 + (12 − 2 × 6) × 1
2 = 2 × (−5) + 12 × 1
return 2, 1, −5
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Compute gcd (26, −14):
Implementation

Summary gcd(26,-14)
gcd(14,12)=2
received 2, 1, −5
2 = 2 × (−5) + 12 × 1
2 = 14 − 12 × 1
2 = (14 − 12 × 1) × (−5) + 12 × 1
2 = 14 × (−5) + 12 × 6
return 2, −5, 6
MAT 685:
C++ for Math-
ematicians
Example
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Compute gcd (26, −14):
Implementation

Summary
gcd(26,-14)=2
received 2, −5, 6
2 = 14 × (−5) + 12 × 6
12 = 26 − 14 × 1
2 = 14 × (−5) + (26 − 14 × 1) × 6
2 = 14 × (−11) + 26 × 6
2 = −14 × 11 + 26 × 6
return 2, 6, 11
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
Return 3 items?
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation
We need gcd() to return gcd (a, b), x, and y
Summary
MAT 685:
C++ for Math-
ematicians
Return 3 items?
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation
We need gcd() to return gcd (a, b), x, and y
Summary

Problem
C++ procedures return only one item

Solution
Modify arguments
MAT 685:
C++ for Math-
ematicians
Modify arguments?
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
To “return” more than one item, modify procedure’s arguments
Implementation

Summary
MAT 685:
C++ for Math-
ematicians
Modify arguments?
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
To “return” more than one item, modify procedure’s arguments
Implementation

Summary
Problem
C++ procedures copy arguments by default
• Can change copy of argument, but. . .
• . . . doesn’t change original!

Solution
Pass by “reference”
MAT 685:
C++ for Math-
ematicians
Pass by reference?
John Perry

Background
Listing 1: template to pass by reference
Implementa-
tion return_type proc_name(type1 & arg1, ...) {
Passing by reference
Overloading statement1;
Implementation
statement2;
Summary
...
}

Notice the difference?


MAT 685:
C++ for Math-
ematicians
Pass by reference?
John Perry

Background
Listing 2: template to pass by reference
Implementa-
tion return_type proc_name(type1 & arg1, ...) {
Passing by reference
Overloading statement1;
Implementation
statement2;
Summary
...
}

Notice the difference?


& pass argument by reference
• argument not copied
• procedure receives actual object
• hides some “pointer magic”
(if you don’t know what pointers are, don’t
worry about it)
MAT 685:
C++ for Math-
ematicians
C++ Interface
John Perry

Background

Implementa-
tion
Listing 3: add to recursive gcd.hpp
Passing by reference
Overloading
/**
Implementation
Calculate the greatest common divisor
Summary
of the first two integers, as well as
the Bezout coefficients.
@param a integer for gcd
@param b integer for gcd
@param x Bezout coefficient (modified)
@param y Bezout coefficient (modified)
@return gcd(a,b)
*/
long gcd(long a, long b, long & x, long & y);
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
An objection
John Perry

Background

Implementa- long gcd(long, long);


tion
Passing by reference
long gcd(long, long, long &, long &);
Overloading
Implementation

Summary Can we do this?


Already defined gcd() in gcd.hpp; can confuse computer?
MAT 685:
C++ for Math-
ematicians
An objection
John Perry

Background

Implementa- long gcd(long, long);


tion
Passing by reference
long gcd(long, long, long &, long &);
Overloading
Implementation

Summary Can we do this?


Already defined gcd() in gcd.hpp; can confuse computer?

• Overloading
• key feature of object-oriented languages
• Simula (1967)
• Many languages do not allow this
• identifiers must be unique
• common in older languages
• Fortran, C, Pascal, . . .
MAT 685:
C++ for Math-
ematicians
Overloading
John Perry

Background

Implementa-
tion • Same name, different procedures
Passing by reference
Overloading • Also called polymorphism
Implementation

Summary
• C++ allows w/different signature, determined by:
• number of arguments
• argument types
MAT 685:
C++ for Math-
ematicians
Overloading
John Perry

Background

Implementa-
tion • Same name, different procedures
Passing by reference
Overloading • Also called polymorphism
Implementation

Summary
• C++ allows w/different signature, determined by:
• number of arguments
• argument types

long gcd(long, long);


long gcd(long, long, long &, long &);

• first gcd() takes two arguments


• second gcd() takes four arguments
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
C++ Implementation
John Perry

Background

Implementa-
Listing 4: add to gcd_recursive.cpp (p. 1)
tion long gcd ( long a , long b , long & x , long & y ) {
Passing by reference
Overloading
// Make s u r e a , b n o n n e g a t i v e
Implementation bool a _ n e g = f a l s e ;
bool b_neg = f a l s e ;
Summary
if ( a < 0) {
a = −a ;
a_neg = true ;
}
if ( b < 0) {
b = −b ;
b_neg = t r u e ;
}

// i f a , b b o t h z e r o p r i n t e r r o r , r e t u r n 0
i f ( ( a == 0 ) and ( b == 0 ) ) {
c e r r << "WARNING: gcd c a l l e d with two z e r o s . \ n " ;
return 0 ;
}
MAT 685:
C++ for Math-
ematicians
C++ Implementation
John Perry

Background
Listing 5: add to gcd_recursive.cpp (p. 2)
Implementa-
i f ( b == 0) {
tion x = 1; y = 0;
Passing by reference return a;
Overloading
}
Implementation
i f ( a == 0) {
Summary x = 0; y = 1;
return b;
}

long c = a % b ;
long q = a / b ;

long d = gcd ( b , c , x , y ) ;
long new_x = y ;
long new_y = x − q ∗y ;
x = new_x ;
y = new_y ;
i f ( a _ n e g ) x = −x ;
i f ( b_neg ) y = −y ;
return d ;
}
MAT 685:
C++ for Math-
ematicians
A basic test program
John Perry

Background

Implementa-
tion Listing 6: test_xgcd.cpp (place in same folder)
Passing by reference
Overloading # i n c l u d e <i o s t r e a m >
Implementation
using s t d : : c i n ; using s t d : : cout ; using s t d : : endl ;
Summary # i n c l u d e " gcd . hpp "

i n t main ( ) {
long a , b , x , y ;
c o u t << " E n t e r two numbers : " << e n d l ;
c i n >> a >> b ;
c o u t << " gc d ( " << a << " , " << b << " ) = " ;
c o u t << gcd ( a , b , x , y ) << " = " ;
c o u t << a << ’ ∗ ’ << x ;
c o u t << " + " << b << ’ ∗ ’ << y << e n d l ;
}
MAT 685:
C++ for Math-
ematicians
Building from command line,
John Perry
running
Background

Implementa-
tion
Passing by reference $ c l a n g++ −c g c d _ r e c u r s i v e . cpp
Overloading $ c l a n g++ −o t e s t _ g c d g c d _ r e c u r s i v e . o t e s t _ x g c d . cpp
Implementation

Summary
MAT 685:
C++ for Math-
ematicians
Building from command line,
John Perry
running
Background

Implementa-
tion
Passing by reference $ c l a n g++ −c g c d _ r e c u r s i v e . cpp
Overloading $ c l a n g++ −o t e s t _ g c d g c d _ r e c u r s i v e . o t e s t _ x g c d . cpp
Implementation

Summary
Usage:
$ ./test_xgcd
Enter two numbers:
26 14
gcd(26,14) = 2 = 26*-1 + 14*2
$ ./test_xgcd
Enter two numbers:
26 -14
gcd(26,-14) = 2 = 26*-1 + -14*-2
MAT 685:
C++ for Math-
ematicians
Careful with references, however!
John Perry
If we change the lines. . .
Background
cout << gcd(a,b,x,y) << " = ";
Implementa-
tion cout << a << ’*’ << x;
Passing by reference
Overloading
Implementation . . . to. . .
Summary
cout << gcd(a,b,x,y) << " = " << a
<< ’*’ << x;

. . . we get a slightly wrong result!


$ ./test_xgcd
Enter two numbers:
26 14
gcd(26,14) = 2 = 26*0 + 14*2

Why?
MAT 685:
C++ for Math-
ematicians
Careful with references, however!
John Perry
If we change the lines. . .
Background
cout << gcd(a,b,x,y) << " = ";
Implementa-
tion cout << a << ’*’ << x;
Passing by reference
Overloading
Implementation . . . to. . .
Summary
cout << gcd(a,b,x,y) << " = " << a
<< ’*’ << x;

. . . we get a slightly wrong result!


$ ./test_xgcd
Enter two numbers:
26 14
gcd(26,14) = 2 = 26*0 + 14*2

Why?

Answer
MAT 685:
C++ for Math-
ematicians
Outline
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
1 Background
Implementation

Summary

2 Implementation
Passing by reference
Overloading
Implementation

3 Summary
MAT 685:
C++ for Math-
ematicians
Summary
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation

Summary • Math stuff


• Extended Euclidean Algorithm
• Programming stuff
• pass by reference
• overloading
MAT 685:
C++ for Math-
ematicians
Homework
John Perry

Background

Implementa-
tion
Passing by reference
Overloading
Implementation

Summary

pp. 49-50 #3.1, 3.7, 3.9

You might also like