McCarthy is a famous theorician of computer science. In his work, he defined a recursive function, called f91, that takes as input a positive integer N and returns a positive integer defined as follows:

  • If N ≤ 100, then f91(N) = f91(f91(N+11));
  • If N ≥ 101, then f91(N) = N-10.

The Problem

Write a program, that computes McCarthy’s f91.

The Input

The input tests will consist of a series of positive integers, each integer is at most 1,000,000. There will be at most 250,000 test cases. Each number is on a line on its own. The end of the input is reached when the number 0 is met. The number 0 shall not be considered as part of the test set.


The program shall output each result on a line by its own, following the format given in the sample output.

Sample input




Sample output

f91(500) = 490

f91(91) = 91


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication1


    class Program


        static void Main(string[] args)


            int num = 500;

            Console.WriteLine("f91(" + num + ") = " + f91(num));


        public static int f91(int n)


            if (n >= 101)


                return (n – 10);




                return f91(f91(n + 11));






f91(500) = 490
Press any key to continue . . .

This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s