Exercise · Volume 1 · 1.1-8
Exercise 1.1-8
not stated yet in Lean solution written
| Rating | M25 |
|---|---|
| Markers | M (mathematical) |
| Kind | K5, design an algorithm: give a method for a task, often with a cost bound from the written solution |
| Section | Section 1.1 of Volume 1. Companion chapter: Algorithms |
The task
Give a method on strings in the form of eq. 1.1(3), with rules , that starts with and ends with .
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Use Euclid's method with subtraction: . In string form, cancel a letter against a letter and count the cancelled pairs.
Hint 2
Count the pairs by a third letter . When no pair is left, the string has the letters and the rest of the longer block.
Hint 3
Rename the letters so that the new string is in some order, and repeat. Stop when nothing is left to cancel.
Solution
Show the solution
Let and . In each rule, the number is the next rule when does not occur, and is the next rule when occurs and is replaced by (the first occurrence). An empty occurs at the left end of the string.
| Action | |||||
|---|---|---|---|---|---|
| 0 | empty | 2 | 1 | Delete one pair . | |
| 1 | empty | 0 | 0 | Put a at the left end, go to 0. | |
| 2 | empty | 4 | 3 | Delete one pair . | |
| 3 | empty | 0 | 0 | Put a at the left end, go to 0. | |
| 4 | 5 | 6 | Test whether an is left. | ||
| 5 | 8 | 7 | Test whether a is left. | ||
| 6 | 0 | 6 | Change every to . Then go to 0. | ||
| 7 | 6 | 7 | Change every to . Then go to 6. | ||
| 8 | 9 | 8 | Change every to . Then stop. |
Idea of a round. Suppose the string at rule 0 has the form or with , and let . Rules 0 to 3 delete an adjacent pair of different letters, and put a at the left, until no pair or is left. The string is then followed by letters of one kind. If and the letters are , rule 4 sends control to rule 6, and rule 6 turns each into : the string is . If and the letters are , rule 4 finds no , rule 5 finds a , rule 7 turns each into , and rule 6 turns each into . The string is again . So the round replaces the pair by and keeps the greatest common divisor, because .
If , no and no is left. Rule 4 goes to rule 5, rule 5 goes to rule 8, and rule 8 turns each into . The string is with , and the method stops at state 9.
Correctness and finiteness. Let at the start of a round. The round ends at the new pair with , since . The number is at least , so each round starts again with two nonempty blocks, or it ends with . The sum drops at each round, so the method makes finitely many rounds. The gcd is the same at each round, and at the last round the pair is with , so the output is . Each loop inside a round removes or renames one letter at each step, so each round ends.
Check
Run the method on , the input for and . Rules 0 and 1 delete two pairs and put two letters at the left, so the string is . Rules 2 and 4 find no and see an , so rule 6 changes both to and the string is . This is the pair in the form . In the second round rule 2 deletes , rule 3 adds a , and the string is . Rules 4 and 5 find no and one , rule 7 changes to and rule 6 changes to , so the string is , the pair . In the third round rule 2 deletes and rule 3 gives . Rules 4 and 5 find neither letter, rule 8 changes to , and the method stops at state 9 with , and . It takes steps. A python3 run of all pairs gave each time, and the input gave after steps.
Remarks
The method uses only subtraction, the form in which Euclid's algorithm is given in the Elements. The C1 chapter of Section 1.1 has a smaller method of the same kind, which deletes all letters .
This solution is checked, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.