Subtraction operation using only increment, loop, assign, zero

徘徊边缘 提交于 2019-11-30 02:16:40

Stephen Cole Kleene devised a way to perform integer subtraction using integer addition. However, it assumes that you cannot have negative integers. For example:

0 - 1 = 0
1 - 1 = 0
2 - 1 = 1
3 - 1 = 2
4 - 1 = 3
5 - 2 = 3
6 - 3 = 3
6 - 4 = 2
6 - 5 = 1
6 - 6 = 0
6 - 7 = 0

In your question, you implemented the addition operation using the increment operation.

Similarly, you can implement the subtraction operation using the decrement operation as follows:

sub(x, y) {
    loop y
        { x = decr(x) }
    return x
}

Now, all we need to do is implement the decrement operation.

This is where the genuis of Kleene shines:

decr(x) {
    y = 0
    z = 0

    loop x {
        y = z
        z = incr(z)
    }

    return y
}

Here we've used all the four operations. This is how it works:

  1. We have two base cases, y (the base case for 0) and z (the base case for 1):

    y = 0 - 1 = 0
    z = 1 - 1 = 0
    

    Hence, we initialize them both to 0.

  2. When x is 0 we run the loop 0 times (i.e. never) and then we simply return y = 0.

  3. When x is 1 then we run the loop once, assign y = z and then simply return y = z = 0.

Notice that every time we run the loop y holds the result of the current iteration while z holds the result of the next iteration. This is the reason why we require two base cases. The decrement function is not a continuous function. It is a piecewise function:

decr(0)     = 0
decr(n + 1) = n

Kleene realized this when he went to the dentist and the dentist extracted two of his teeth. He was frustrated while trying to solve this very problem and when the dentist extracted two of his teeth he realized that he required two base cases.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!