0% found this document useful (0 votes)
3 views12 pages

mooc-02 (2)

The document explains the concept of the greatest common divisor (GCD) and presents two methods for calculating it: a naive approach and Euclid's algorithm. The naive method is slow and inefficient for large numbers, while Euclid's method is based on the principle that gcd(a, b) is the same as gcd(b, a % b). The document also discusses variable assignment and the execution flow of the algorithm.

Uploaded by

somanip8970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

mooc-02 (2)

The document explains the concept of the greatest common divisor (GCD) and presents two methods for calculating it: a naive approach and Euclid's algorithm. The naive method is slow and inefficient for large numbers, while Euclid's method is based on the principle that gcd(a, b) is the same as gcd(b, a % b). The document also discusses variable assignment and the execution flow of the algorithm.

Uploaded by

somanip8970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

GCD

 An algorithm to find the greatest common divisor of two


positive integers m and n, m ≥ n.
 A naïve solution – Described informally as follows.
1. Take the smaller number n.
2. For each number k, n ≥k≥1, in descending order, do the following.
1. If k divides m and n, then k is the gcd of m and n

 This will compute gcd correctly, but is VERY slow (think


about large numbers m and n).
 There is a faster way…

1
GCD Algorithm - Intuition
To find gcd of 8 and 6. Consider rods of length 8 and 6. Measure
the longer with the shorter. Take the remainder if any. Repeat the
process until the longer can be exactly measured as an integer
multiple of the shorter.
Remainder

Gcd(8, 6) = 2.

2
GCD Algorithm - Intuition

102
102 mod 21 = 18
21
21 mod 18 = 3
18

Gcd (102, 21) = 3

3
Euclid’s method for gcd
Euclid’s algorithm (step-by-step method for
calculating gcd) is based on the following
simple fact.

Suppose a > b. Then the gcd of a and b is


the same as the gcd of b and the
remainder of a when divided by b.

gcd(a,b) = gcd (b, a % b)

To see this consider division of a by b


a = bq + r
4
Euclid’s gcd Input
a, b

a,b,g are Exchange


variables. b≤a (a,b)
Variables NO
“store” YES
exactly one
value at a b==0 Print a HALT
time. ? YES
NO

g= a%b Exchange(a,b)
a=b t= a
a%b is the remainder b=g a=b
when a is divided by b. b=t
Eg. 8%3 is 2 5
Variables and Assigning them
Input
 Concept of
a, b variable: a name
for a box.
 a,b,g are variables
Exchan
b≤a ge(a,b) that are names for
? NO integer boxes. b
a 5 3
YES

b== Print a Halt


0? YES  Assignment a = b
NO replaces whatever
g= a%b is stored in a by
a=b what is stored in b.
b=g
 After a = b

a 3 3 b
6
Sequential assignments
g = a%b;
 Semi-colons give a
a = b; sequential order in
b = g; which to apply the
statements.
 Variables are boxes
initially
After
After
a =b
g b= g
a %b
to which a name is
a b g
given.
10
6 4
6 ??
4
 We have 3
variables: a, b, g.
This gives us three
boxes. Initially, a is
10, b is 6 and g is
undefined.
 Run statements in
sequence. 7
Running the program
Input Program counter. At the next step
a, b to be executed. Initially at beginning.

State of the program is variables :


Exchan boxes with names.
b≤a ge(a,b)
? NO
YES
a b g
b== Print a Halt
Now let us start running the flowchar
0? YES One step at a time.
NO
1. After input step:
g= a%b
a=b
b=g 8 6 ??
a b g

8
Tracing the execution
Input
Now
Program
let us Counter
start running
is at the
thenext
flowchart.
step to
a, b Always
be runone box at a time.

3.
1.7.5.After
Test
Testinput
Test b==0?
b==0?step:
b==0? NO
YES
NO
Exchan
b≤a ge(a,b)
? NO 826 602 ??
02
YES
aaa bbb ggg
8. Print a
b== Print a Halt
0? 2
YES
NO 2.
4. Test
6. g=a%b;
b < a=b;
a? YES
b=g;
g= a%b
8
6
2 6
2
0 ??
2
0
a=b
b=g a b g

9
Multiple solutions and comparing them
Input
 Multiple solutions are
a, b possible for the same
problem.

Exchan
b≤a ge(a,b)
 Is the adjacent flowchart
? NO correct for gcd?
YES

b== Print a Halt


 How many times did we
0? YES run in the loop? The
NO fewer the better.
g= a-b
a=b
b=g
 Is it seriously more: by
an order of magnitude?
Notion of complexity. 10
Another solution
 A (slower) alternative. How many times does the loop iterate?

Input
a, b

Exchan
b≤a ge(a,b)
? NO
YES

b== Print a Halt


0? YES
NO
a= a-b
11
Acknowledgments: This lecture slide is based on the material prepared by Prof.
Sumit Ganguly, CSE, IIT Kanpur. The slide design is based on a template by Prof.
Krithika Venkataramani.

You might also like