mccarthy.py


Below is the syntax highlighted version of mccarthy.py from §2.3 Recursion.


#-----------------------------------------------------------------------
# mccarthy.py
#-----------------------------------------------------------------------

import stdio
import sys

#-----------------------------------------------------------------------

# Return McCarthy's 91 function at n.

def mcCarthy(n):
    if n > 100:
        return n - 10
    return mcCarthy(mcCarthy(n+11))

#-----------------------------------------------------------------------

# Accept an integer command-line argument n, and compute and write
# McCarthy's 91 function at n.

def main():
    n = int(sys.argv[1])
    stdio.writeln(mcCarthy(n))

if __name__ == '__main__':
    main()

#-----------------------------------------------------------------------

# $ python mccarthy.py 0
# 91

# $ python mccarthy.py 10
# 91

# $ python mccarthy.py 90
# 91

# $ python mccarthy.py 91
# 91

# $ python mccarthy.py 99
# 91

# $ python mccarthy.py 100
# 91

# $ python mccarthy.py 101
# 91

# $ python mccarthy.py 102
# 92

# $ python mccarthy.py 103
# 93

# $ python mccarthy.py 104
# 94


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.