Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the...

20
Discrete Structures Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 4.8 Application: Algorithms Begin at the beginning…and go on till you come to the end: then stop. – Lewis Carroll, 1832 – 1898 Alice’s Adventures in Wonderland, 1865

Transcript of Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the...

Page 1: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 1

Discrete Structures

Chapter 4: Elementary Number Theory and Methods of Proof

4.8 Application: Algorithms

Begin at the beginning…and go on till you come to the end: then stop.

– Lewis Carroll, 1832 – 1898 Alice’s Adventures in Wonderland, 1865

Page 2: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 2

Definitions

• Variable

In higher-level computer languages, the term variable is used to refer to a specific storage location in a computer’s memory.

• Data Type

The data type of a variable indicates the set in which the variables takes its values.

• Assignment Statement

An assignment statement gives a value to the variable in the form x: = e where x is the variable and e is the expression.

Page 3: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 3

Conditional Statements

Ordinarily, algorithm statements are executed one after another in the order in which they are written. Conditional statements allow this natural order to be overridden by using the current values of program variables to determine which algorithm statement will be executed next.

Page 4: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 4

Conditional Statements

Conditional statements are denoted in one of two ways.

1. If (condition)then s1

else s2

2. If (condition) then s1

Notice that we use indentation to indicate that the statements belong together. We can also bind statements by do and ending with end do.

Page 5: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 5

Execution of if-then-else statements

1. The condition is evaluated by substituting the current values of all algorithm values appearing in it and evaluating the truth or falsity of the resulting statement.

2. If the condition is true, then s1 is executed and execution moves to the next algorithm statement following the if-then-else statement.

3. If the condition is false, then s2 is executed and execution moves to the next algorithm statement following the if-then-else statement.

Note: Sometimes condition is called guard because it is stationed before s1 and s2 and restricts access to them.

Page 6: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 6

Examples – pg. 225 # 1 & 2

• Find the value of z when each of the algorithm segments is executed.

2. i := 3if (i 3 or i > 6)

then z := 2else z := 0

1. i := 2if (i > 3 or i 0)

then z := 1else z := 0

Page 7: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 7

Iterative Statements

Iterative statements are used when a sequence of algorithm statements is to be executed over and over again.

We use two types of iterative statements:

while loops and for-next loops.

Page 8: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 8

While Loop

A while loop has the form

while (condition)

[ statements that make up the body of the loop]

end while

Page 9: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 9

Execution of while Loop

1. The condition is evaluated by substituting the current values of all algorithm variables and evaluating the truth or falsity of the resulting statement.

2. If the condition is true, all statements in the body of the loop are executed in order. Then execution moves back to the beginning of the loop and the process repeats.

3. If the condition is false, execution passes to the next algorithm statement following the loop.

Note: Each execution of the body of the loop is called an iteration of the loop.

Page 10: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 10

Example - While Loop

i := 10

while (i > 0)

display i

i := i – 1

end while

display “Blast Off”

Page 11: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 11

for-next Loop

A for-next loop has the form

for variable := initial expression to final expression

[ statements that make up the body of the loop]

next (same) variable

Page 12: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 12

Execution of for-next Loop

1. The for-next loop variable is set equal to the value of the initial expression.

2. A check is made to determine whether the value of variables is less than or equal to the value of the final expression.

3. If the value of the variable is less than or equal to the value of the final expression, then the statements in the body of the loop are executed in order, variable is increased by 1, and execution returns back to step 2.

4. If the value of the variable is greater than the value of the final expression, then execution passes to the next algorithm statement following the loop.

Page 13: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 13

Example – pg 225 # 4

Find the values of a after execution of the loop.

a := 2

for i := 1 to 2

a := a/2 + 1/a

next i

Page 14: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 14

Algorithm Notation

We generally include the following information when describing algorithms.1. The name of the algorithm, together with a list of

input and output variables.

2. A brief description of how the algorithm works.

3. The input variable names, labeled by data type (integer, real number, etc.).

4. The statements that make up the body of the algorithm, possibly with explanatory comments.

5. The output variable names, labeled by data type.

Page 15: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 15

Algorithm 4.8.1 – Division Algorithm

Page 16: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 16

Example – pg 225 # 6

Make a trace table to trace the action of Algorithm 4.8.1 for the given input values.

6. a = 26

d = 7

Page 17: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 17

Definition – Greatest Common Divisor

Let a and b be integers that are not both zero. The greatest common divisor of a and b, denoted gcd(a, b), is that integer d with the following properties:

1. d is a common divisor of both a and b. In other words, d | a and d | b.

2. For all integers c, if c is a common divisor of both a and b, then c is less than or equal to d. In other words, for all integers c, if c | a and c | b, then c d.

Page 18: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 18

Lemmas

• Lemma 4.8.1

If r is a positive integer, then gcd(r, 0) = r.

• Lemma 4.8.2

If a and b are any integers not both zero, and if q and r are any integers s.t. a = bq + r, then

gcd(a, b) = gcd(b, r).

Page 19: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 19

Algorithm 4.8.2 – Euclidean Algorithm

Page 20: Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

4.8 Application: Algorithms 20

Example – pg 225 # 17

Make a trace table to trace the action of Algorithm 4.8.2 for the given input values.

17. 1,001 and 871