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:

    Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.
    Herman Melville (1819–1891)

    Acknowledge your will and speak to us all, “This alone is what I will to be!” Hang your own penal code up above you: we want to be its enforcers!
    Friedrich Nietzsche (1844–1900)