Exercise · Volume 1 · 2.2.4-9
Exercise 2.2.4-9
not stated yet in Lean solution written
| Rating | 23 |
|---|---|
| Kind | K2, decide: say whether a claim holds from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
Decide whether Algorithm A works when , and whether Algorithm M works when , when and when .
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
Trace each algorithm with a small example, and ask which node is changed or freed while the other pointer still needs it.
Hint 2
Remember that a term whose sum of coefficients is is removed from the list.
Solution
Show the solution
A with . Each term is added to itself, so its coefficient is doubled, and the answer is , as it should be. The coefficient would become only if it was already, which a proper polynomial never has. If a stored term has coefficient and a non-negative , the algorithm frees the node that still needs, and it fails.
M with . Algorithm M only adds shifted copies of to and never changes and . So it works, and .
M with . Now changes during the algorithm. If is the sum of the terms of , then after the cycle for the polynomial is multiplied by . So the result is , and not .
M with . Now the list that is read as the multiplier is changed during the run. Look at the cycle for the term of . It adds the terms to for the terms of . Every has , so every changed or new term of has an exponent at least as large as that of , and in the decreasing order these terms lie at or before it. The terms that are still to be read lie after and are not touched. The coefficient of itself changes only in the last step of the cycle, when the constant term of is added, after the product that uses it. So the loop reads the original multiplier, and the result is , as it should be. It fails when the constant term of is . Then the term cancels in the last step, its node is freed, and the next step of the loop follows the link of a freed node.
Check
On the simulator, with , and , the result was . With and , , the result was , and with it was . With and , the result was . With the run gave instead of . Algorithm A with gave , and with a stored zero coefficient the run failed.
Remarks
The safe rule is to use different lists for the multiplier, the multiplicand and the result, or to copy a list first with the subroutine of Exercise 11.
This solution is drafted, 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.