Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 stop. – Lewis Carroll, 1832 – 1898 Alice’s Adventures in Wonderland, 1865

2 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: Algorithms2

3 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: Algorithms3

4 Conditional statements are denoted in one of two ways. 1.If (condition) then s 1 else s 2 2.If (condition) then s 1 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: Algorithms4

5 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 s 1 is executed and execution moves to the next algorithm statement following the if-then- else statement. 3.If the condition is false, then s 2 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 s 1 and s 2 and restricts access to them. 4.8 Application: Algorithms5

6 Find the value of z when each of the algorithm segments is executed. 4.8 Application: Algorithms6 2. i := 3 if (i  3 or i > 6) then z := 2 else z := 0 1. i := 2 if (i > 3 or i  0) then z := 1 else z := 0

7 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: Algorithms7

8 A while loop has the form while (condition) [ statements that make up the body of the loop] end while 4.8 Application: Algorithms8

9 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. 4.8 Application: Algorithms9

10 i := 10 while (i > 0) display i i := i – 1 end while display “Blast Off” 4.8 Application: Algorithms10

11 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 4.8 Application: Algorithms11

12 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. 4.8 Application: Algorithms12

13 Find the values of a after execution of the loop. a := 2 for i := 1 to 2 a := a/2 + 1/a next i 4.8 Application: Algorithms13

14 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. 4.8 Application: Algorithms14

15 4.8 Application: Algorithms15

16 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: Algorithms16

17 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. 4.8 Application: Algorithms17

18 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: Algorithms18

19 4.8 Application: Algorithms19

20 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: Algorithms20


Download ppt "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."

Similar presentations


Ads by Google