McCarthy 91 Function - Code

Code

Here is an implementation of the recursive algorithm in Lisp:

(defun mc91 (n) (cond ((<= n 100) (mc91 (mc91 (+ n 11)))) (t (- n 10))))

Here is an implementation of the recursive algorithm in C:

int mc91(int n) { if (n > 100) { return n - 10; } else { return mc91(mc91(n+11)); } }

Here is an implementation of the non-recursive algorithm in C:

int mccarthy(int n) { int c; for (c = 1; c != 0; ) { if (n > 100) { n = n - 10; c--; } else { n = n + 11; c++; } } return n; }

Read more about this topic:  McCarthy 91 Function

Famous quotes containing the word code:

    ... the self respect of individuals ought to make them demand of their leaders conformity with an agreed-upon code of ethics and moral conduct.
    Mary Barnett Gilson (1877–?)

    Motion or change, and identity or rest, are the first and second secrets of nature: Motion and Rest. The whole code of her laws may be written on the thumbnail, or the signet of a ring.
    Ralph Waldo Emerson (1803–1882)

    ...I had grown up in a world that was dominated by immature age. Not by vigorous immaturity, but by immaturity that was old and tired and prudent, that loved ritual and rubric, and was utterly wanting in curiosity about the new and the strange. Its era has passed away, and the world it made has crumbled around us. Its finest creation, a code of manners, has been ridiculed and discarded.
    Ellen Glasgow (1873–1945)