1
00:00:02,700 --> 00:00:08,740
We will see a few more examples, because loops
are really important.
2
00:00:08,740 --> 00:00:15,750
Let us go back to the first problem that we
discussed, which was the problem of computing
3
00:00:15,750 --> 00:00:21,450
the greatest common divisor of two positive
numbers.
4
00:00:21,450 --> 00:00:28,590
So, the problem is to read the two numbers,
find their GCD and compute the output.
5
00:00:28,590 --> 00:00:34,580
Now, we had a flowchart of the problem based
on the fact that, if a is greater than or
6
00:00:34,580 --> 00:00:43,280
equal to b, then GCD of a comma 0 if b is
0, then GCD of a comma 0 is a.
7
00:00:43,280 --> 00:00:53,251
Otherwise, GCD of a comma b is the same as
GCD of b comma a modulo b, with a modulo b
8
00:00:53,251 --> 00:00:57,790
is a percentage b is the remainder, when a
is divided by b.
9
00:00:57,790 --> 00:01:07,289
So, let us now try to write the program in
C using a while loop.
10
00:01:07,289 --> 00:01:10,259
So, we have to do a few preliminary things.
11
00:01:10,259 --> 00:01:15,409
So, let us call up the first half of the program.
12
00:01:15,409 --> 00:01:22,460
In the first half I declare three variables,
a, b and there is another variable t, whose
13
00:01:22,460 --> 00:01:24,210
need we will see right now.
14
00:01:24,210 --> 00:01:30,530
But, let us say that I need an extra variable
for now, let us just take it on faith.
15
00:01:30,530 --> 00:01:34,170
So, what I will do is scan two variables a
and b.
16
00:01:34,170 --> 00:01:43,319
Now, recall in the GCD equation that we saw
right now, we assume that a is greater than
17
00:01:43,319 --> 00:01:44,319
or equal to b.
18
00:01:44,319 --> 00:01:53,060
Now, what if the user is unaware of his condition
and enter the lesser number first.
19
00:01:53,060 --> 00:01:58,810
So, he just entered the numbers in such a
way that, a is less than b.
20
00:01:58,810 --> 00:02:03,819
So, we need to correct that, we need to make
sure that a is the greater number.
21
00:02:03,819 --> 00:02:12,050
So, we need to exchange the values of a and
b, if it is true that a is less then b.
22
00:02:12,050 --> 00:02:14,840
Now, how do we do this?
23
00:02:14,840 --> 00:02:25,650
So, the first thing to note to try will be
to say that for example, if I say that let
24
00:02:25,650 --> 00:02:33,580
us say a was 3 and b was 4.
25
00:02:33,580 --> 00:02:41,469
And suppose, I just said a equal to b I want
to exchange the values of a and b.
26
00:02:41,469 --> 00:02:45,959
I just said a equal to b and b equal to a.
27
00:02:45,959 --> 00:02:47,560
What will be the effect of this?
28
00:02:47,560 --> 00:02:50,579
Note that, this is the assignment statement.
29
00:02:50,579 --> 00:03:04,780
So, after I execute this line, b is 4 so,
a will be 4, a equal to b results in a equal
30
00:03:04,780 --> 00:03:11,500
to 4.
31
00:03:11,500 --> 00:03:13,150
After the execution of this line.
32
00:03:13,150 --> 00:03:18,519
So, what this situation that we will have
is that b equal to 4 and a equal to 4.
33
00:03:18,519 --> 00:03:23,049
And we will have no memory of, what was the
original value of a?
34
00:03:23,049 --> 00:03:24,180
That is lost.
35
00:03:24,180 --> 00:03:27,130
So, it is just simply lost.
36
00:03:27,130 --> 00:03:34,819
So, this idea that we can exchange two values
by just writing a equal b, b equal to a does
37
00:03:34,819 --> 00:03:35,950
not work.
38
00:03:35,950 --> 00:03:39,720
So, what is the correct way to do it?
39
00:03:39,720 --> 00:03:49,959
So, we have an idea known as the cyclic exchange
and this is a really neat idea.
40
00:03:49,959 --> 00:03:56,530
The idea is thatÉ So, how can I motivate
it?
41
00:03:56,530 --> 00:04:00,930
Let us say that you have two rooms and these
two rooms are full of steps.
42
00:04:00,930 --> 00:04:06,290
And I want to change the contents of the first
room to the second and the second room to
43
00:04:06,290 --> 00:04:07,290
the third.
44
00:04:07,290 --> 00:04:12,810
One way I can do it is that I will move the
contents of the first room to a different
45
00:04:12,810 --> 00:04:13,810
room.
46
00:04:13,810 --> 00:04:19,610
So, have a temporary room and then copy the
contents of the second room to the first and
47
00:04:19,610 --> 00:04:22,950
copy the contents of the third room to the
second.
48
00:04:22,950 --> 00:04:27,840
So, this is a very nice intuition and it almost
is similar to what we need to do.
49
00:04:27,840 --> 00:04:33,910
So, we have two variables a and b that we
need to swap.
50
00:04:33,910 --> 00:04:39,260
So, one thing we can do is, I will keep a
third variable t.
51
00:04:39,260 --> 00:04:44,100
First what I will do is, I will copy the value
of a to t.
52
00:04:44,100 --> 00:04:47,090
So, now I have a backup copy.
53
00:04:47,090 --> 00:04:51,960
Now, then I will say a equal to b,.
54
00:04:51,960 --> 00:05:00,600
So, at this point the value of b will be copy
to the a, So, now, a is 6, b is 6.
55
00:05:00,600 --> 00:05:06,180
But, still we have a memory of what was a
before?
56
00:05:06,180 --> 00:05:09,820
Because, the old value of a was told in t.
57
00:05:09,820 --> 00:05:16,040
So, in order to complete the routine, all
you need to do is to copy the value of t to
58
00:05:16,040 --> 00:05:22,500
b and that can be a complete by the equation
by the assignment statement b equal to t.
59
00:05:22,500 --> 00:05:25,750
So, this idea is known as cyclic exchange.
60
00:05:25,750 --> 00:05:31,970
So, recall the physical intuition of swapping
the contents of two rooms which are almost
61
00:05:31,970 --> 00:05:32,970
full.
62
00:05:32,970 --> 00:05:39,830
You have a third room and you move the contents
of the first room to the third room, move
63
00:05:39,830 --> 00:05:44,620
the contents of the second to the first and
move the contents to the third to the second.
64
00:05:44,620 --> 00:05:46,970
So, this is similar to what we did.
65
00:05:46,970 --> 00:05:52,710
In the case of physical contents, we cannot
copy, in the case of variables, we can copy.
66
00:05:52,710 --> 00:05:55,710
So, now let us complete the GCD program.
67
00:05:55,710 --> 00:06:01,780
We have just done the first part of the program
which is to ensure that a is actually the
68
00:06:01,780 --> 00:06:03,490
greater number.
69
00:06:03,490 --> 00:06:07,880
If it was not the greater number, you swap
or exchange.
70
00:06:07,880 --> 00:06:13,160
Now, So, after exchange we have ensure that
a is greater than or equal to b.
71
00:06:13,160 --> 00:06:20,800
Now, we have to write the main loop for the
GCD function.
72
00:06:20,800 --> 00:06:30,840
So, just by translating the flowchart, what
we will do is while b is not 0.
73
00:06:30,840 --> 00:06:36,830
What you do is, you say that store the value
of a and t.
74
00:06:36,830 --> 00:06:43,760
Assign a to b, a equal to b and b becomes
t modulo b.
75
00:06:43,760 --> 00:06:46,910
Recall the equation was written as follows
that.
76
00:06:46,910 --> 00:06:58,021
So, recall that the equation was GCD of a,
b is the same as GCD of b, a modulo b.
77
00:06:58,021 --> 00:07:09,930
So, when you assign a to b, the old value
of a is lost and we can no longer do a modulo
78
00:07:09,930 --> 00:07:10,930
b.
79
00:07:10,930 --> 00:07:16,430
So, the way to do that is, you use the idea
of temporary variable, store the old value
80
00:07:16,430 --> 00:07:19,301
of a and t, before you do a equal to b.
81
00:07:19,301 --> 00:07:24,930
So, that finally, a modulo b can be done by
t modulo b.
82
00:07:24,930 --> 00:07:30,100
I do not want the new value of a, I want the
old value of a.
83
00:07:30,100 --> 00:07:33,960
So, let us just trace the execution of this
program.
84
00:07:33,960 --> 00:07:41,540
Let us say that I scan two numbers a and b
and the user was correct in entering it.
85
00:07:41,540 --> 00:07:45,270
So, he actually enter the greater number first.
86
00:07:45,270 --> 00:07:52,650
So, we have 16 and 9, a equal to 16 and b
equal to 9 and t is undefined.
87
00:07:52,650 --> 00:08:06,210
So, after you read the numbers, you just say
GCD ofÉ After these you enter a message which
88
00:08:06,210 --> 00:08:09,830
is printed message which is the GCD of a and
b is,.
89
00:08:09,830 --> 00:08:14,110
So, GCD of 16 and 9 is and then you enter
the loop.
90
00:08:14,110 --> 00:08:21,960
So, in the initial execution of the loop you
have t equal to a, which will store t equal
91
00:08:21,960 --> 00:08:28,460
to 16, a equal to b, which is a will become
9.
92
00:08:28,460 --> 00:08:36,180
And then, but you want to compute the modulo
16 modulo 9, but 16 was lost in a, because
93
00:08:36,180 --> 00:08:37,310
a is not 9.
94
00:08:37,310 --> 00:08:39,590
So, you have do t modulo b,.
95
00:08:39,590 --> 00:08:43,320
So, 16 modulo 9 which is 7.
96
00:08:43,320 --> 00:08:50,140
So, you go back to the while loop and then
you see that b is not 0, b is 7.
97
00:08:50,140 --> 00:08:52,120
So, you enter the loop again.
98
00:08:52,120 --> 00:08:54,120
T is equal to a,.
99
00:08:54,120 --> 00:08:56,860
So, t is 9, a equal to b,.
100
00:08:56,860 --> 00:09:02,920
So, a become 7, b becomes 2.
101
00:09:02,920 --> 00:09:05,590
Again b is not 0, So, you enter the loop again.
102
00:09:05,590 --> 00:09:11,920
So, t is 7, a equal to 2 and b equal to 1.
103
00:09:11,920 --> 00:09:24,360
Again you enter the loop, b is not 0, t is
2, a becomes 1 and b becomes 0 at this point,
104
00:09:24,360 --> 00:09:33,020
you exit the loop and at this point a that
we ended with is the GCD of these numbers.
105
00:09:33,020 --> 00:09:35,680
So, 16 and 9 are relatively prime.
106
00:09:35,680 --> 00:09:38,000
Therefore, their GCD is 1.
107
00:09:38,000 --> 00:09:42,360
Now, let us think a minute about what is the
loop invariant in this program?
108
00:09:42,360 --> 00:09:43,360
What is it that?
109
00:09:43,360 --> 00:09:46,490
We have a central while loop which computes
the GCD.
110
00:09:46,490 --> 00:09:50,550
What was the invariant in that loop?
111
00:09:50,550 --> 00:09:56,320
So, for this I will just introduce a slight
notation which makes it easier to discuss
112
00:09:56,320 --> 00:09:58,530
this invariant.
113
00:09:58,530 --> 00:10:06,270
So, let capital A and capital B be the original
numbers that I input.
114
00:10:06,270 --> 00:10:12,990
And little a and little b represent the numbers
which are involved in the loop.
115
00:10:12,990 --> 00:10:18,990
So, capital A and capital B are the original
input and the invariant that I have is that
116
00:10:18,990 --> 00:10:28,170
at every stage, the GCD of the original inputs
are the same as the GCD of little a and b.
117
00:10:28,170 --> 00:10:31,740
We call that little a and b are the loop variables
involved in the loop.
118
00:10:31,740 --> 00:10:37,930
So, little a and b keep changing through the
loop, whereas, capital A and B are fixed,
119
00:10:37,930 --> 00:10:39,750
they are the input.
120
00:10:39,750 --> 00:10:47,520
So, the invariant that I have is that every
time you enter the loop, the GCD of the input
121
00:10:47,520 --> 00:10:50,590
where the same as the GCD of the variable.
122
00:10:50,590 --> 00:10:53,910
Now, this guarantees the correctness.
123
00:10:53,910 --> 00:10:58,800
Because, when you exit out of the loop, you
will correctly compute.
124
00:10:58,800 --> 00:11:04,300
You exit out of the loop, because b equal
to 0, and by the original equation, you know
125
00:11:04,300 --> 00:11:08,040
that when b is equal to 0, a is the GCD of
a and b.
126
00:11:08,040 --> 00:11:11,450
So, this guarantee is correctness.
127
00:11:11,450 --> 00:11:15,860
Now, you could also ask other questions like,
how many times has the loop run?
128
00:11:15,860 --> 00:11:21,500
And this question is big complicated, because
you have to compute it based on the input
129
00:11:21,500 --> 00:11:22,500
numbers.
130
00:11:22,500 --> 00:11:25,550
So, such questions are of interest to computer
science.
131
00:11:25,550 --> 00:11:31,000
But, we will not going to computing the efficiency
or the performance of this code.
132
00:11:31,000 --> 00:11:32,930
But, it is also a very crucial question.