Turing Machine Examples Lecture 27 Section 9.2 Robb T. Koether Hampden-Sydney College Mon, Oct 31, 2016 Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 1 / 17 1 Examples of Turing Machines ADD – Add Two Integers MULT – Multiply Two Integers SQRT – Square Root 2 Assignment Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 2 / 17 Outline 1 Examples of Turing Machines ADD – Add Two Integers MULT – Multiply Two Integers SQRT – Square Root 2 Assignment Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 3 / 17 Outline 1 Examples of Turing Machines ADD – Add Two Integers MULT – Multiply Two Integers SQRT – Square Root 2 Assignment Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 4 / 17 ADD – Add Two Integers Example (ADD – Add Two Integers) Design a Turing machine ADD that will add two nonnegative integers, replacing them with their sum. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 5 / 17 Move right to n2. If n2 is not 0, then call DECR. Move left to n1. Call INCR. Repeat until n2 is 0. Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 If n2 is not 0, then call DECR. Move left to n1. Call INCR. Repeat until n2 is 0. Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 Move left to n1. Call INCR. Repeat until n2 is 0. Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. If n2 is not 0, then call DECR. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 Call INCR. Repeat until n2 is 0. Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. If n2 is not 0, then call DECR. Move left to n1. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 Repeat until n2 is 0. Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. If n2 is not 0, then call DECR. Move left to n1. Call INCR. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 Erase n2. ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. If n2 is not 0, then call DECR. Move left to n1. Call INCR. Repeat until n2 is 0. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 ADD – Add Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. If n2 is not 0, then call DECR. Move left to n1. Call INCR. Repeat until n2 is 0. Erase n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 6 / 17 ADD – Add Two Integers Example (Strategy) x INCR sum x, y = 0 y COMPARE DECR ≠ Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 7 / 17 Outline 1 Examples of Turing Machines ADD – Add Two Integers MULT – Multiply Two Integers SQRT – Square Root 2 Assignment Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 8 / 17 MULT – Multiply Two Integers Example (MULT – Multiply Two Integers) Design a Turing machine MULT that will multiply two nonnegative integers and replace them with their product. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 9 / 17 Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Call INCR. Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Repeat until n2 is 0. Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Erase n2. MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 MULT – Multiply Two Integers Example (Strategy) Label the integers n1 and n2. Move right to n2. Call COPY. (Label the copy n3.) Move right past n3. Write 0. (Label it n4.) Move left to n3. Call ADD. (Compute n3 + n4; n4 is gone.) Move left to n1teger. Call INCR. Repeat until n2 is 0. Erase n2. Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 10 / 17 Write 0 n1 n2 0 COPY n1 n2 0 n2 ADD n1 n2 n2 DECR n1 − 1 n2 n2 COPY n1 − 1 n2 n2 n2 ADD n1 − 1 n2 2n2 DECR n1 − 2 n2 2n2 . DECR 1 n2 (n1 − 1)n2 COPY 1 n2 (n1 − 1)n2 n2 ADD 1 n2 n1n2 DECR 0 n2 n1n2 ERASE n1n2 MULT – Multiply Two Integers Example (Demo) Action Tape Input n1 n2 Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 11 / 17 COPY n1 n2 0 n2 ADD n1 n2 n2 DECR n1 − 1 n2 n2 COPY n1 − 1 n2 n2 n2 ADD n1 − 1 n2 2n2 DECR n1 − 2 n2 2n2 . DECR 1 n2 (n1 − 1)n2 COPY 1 n2 (n1 − 1)n2 n2 ADD 1 n2 n1n2 DECR 0 n2 n1n2 ERASE n1n2 MULT – Multiply Two Integers Example (Demo) Action Tape Input n1 n2 Write 0 n1 n2 0 Robb T. Koether (Hampden-Sydney College) Turing Machine Examples Mon, Oct 31, 2016 11 / 17 ADD n1 n2 n2 DECR n1 − 1 n2 n2 COPY n1 − 1 n2 n2 n2 ADD n1 − 1 n2 2n2 DECR n1 − 2 n2 2n2 .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages55 Page
-
File Size-