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)

    Hollywood keeps before its child audiences a string of glorified young heroes, everyone of whom is an unhesitating and violent Anarchist. His one answer to everything that annoys him or disparages his country or his parents or his young lady or his personal code of manly conduct is to give the offender a “sock” in the jaw.... My observation leads me to believe that it is not the virtuous people who are good at socking jaws.
    George Bernard Shaw (1856–1950)

    Many people will say to working mothers, in effect, “I don’t think you can have it all.” The phrase for “have it all” is code for “have your cake and eat it too.” What these people really mean is that achievement in the workplace has always come at a price—usually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.
    Anne C. Weisberg (20th century)