200 likes | 344 Views
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. Definitions. Variable
E N D
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 4.8 Application: Algorithms
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. 4.8 Application: Algorithms
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. 4.8 Application: Algorithms
Conditional Statements Conditional statements are denoted in one of two ways. • If (condition) thens1 elses2 • If (condition) thens1 Notice that we use indentation to indicate that the statements belong together. We can also bind statements by do and ending with end do. 4.8 Application: Algorithms
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. • If the condition is true, then s1 is executed and execution moves to the next algorithm statement following the if-then-else statement. • 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. 4.8 Application: Algorithms
Examples – pg. 225 # 1 & 2 • Find the value of z when each of the algorithm segments is executed. 1. i := 2 if (i> 3 or i 0) thenz := 1 else z := 0 2. i := 3 if (i 3 or i> 6) thenz := 2 else z := 0 4.8 Application: Algorithms
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. 4.8 Application: Algorithms
While Loop A while loop has the form while (condition) [ statements that make up the body of the loop] end while 4.8 Application: Algorithms
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. • 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. • 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. 4.8 Application: Algorithms
Example - While Loop i:= 10 while (i> 0) display i i:= i – 1 end while display “Blast Off” 4.8 Application: Algorithms
for-next Loop A for-nextloop has the form forvariable := initial expression tofinal expression [ statements that make up the body of the loop] next (same) variable 4.8 Application: Algorithms
Execution of for-next Loop 1. The for-next loop variable is set equal to the value of the initial expression. • A check is made to determine whether the value of variables is less than or equal to the value of the final expression. • 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. • 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. 4.8 Application: Algorithms
Example – pg 225 # 4 Find the values of a after execution of the loop. a := 2 fori:= 1 to 2 a := a/2 + 1/a next i 4.8 Application: Algorithms
Algorithm Notation We generally include the following information when describing algorithms. • The name of the algorithm, together with a list of input and output variables. • A brief description of how the algorithm works. • The input variable names, labeled by data type (integer, real number, etc.). • The statements that make up the body of the algorithm, possibly with explanatory comments. • The output variable names, labeled by data type. 4.8 Application: Algorithms
Algorithm 4.8.1 – Division Algorithm 4.8 Application: Algorithms
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 4.8 Application: Algorithms
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: • d is a common divisor of both a and b. In other words, d | a and d | b. • 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. 4.8 Application: Algorithms
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). 4.8 Application: Algorithms
Algorithm 4.8.2 – Euclidean Algorithm 4.8 Application: Algorithms
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 4.8 Application: Algorithms