What value would the following function return for the input x = 95? Function…
1998
What value would the following function return for the input x = 95?
Function fun (x: integer): integer;
Begin
If x > 100 then fun = x − 10
Else fun := fun(fun(x + 11))
End;
- A.
89
- B.
90
- C.
91
- D.
92
Attempted by 65 students.
Show answer & explanation
Correct answer: C
This recursive function behaves similarly to the McCarthy 91 function.
The function definition is:
If x > 100
fun(x) = x − 10
Else
fun(x) = fun(fun(x + 11))Given:
x = 95Since:
95 ≤ 100We apply:
fun(95) = fun(fun(106))Now:
fun(106) = 106 − 10 = 96So:
fun(95) = fun(96)Again:
96 ≤ 100Thus:
fun(96) = fun(fun(107))Now:
fun(107) = 107 − 10 = 97So:
fun(96) = fun(97)Continuing this recursive process eventually gives:
fun(x) = 91for all values:
x ≤ 100Therefore:
fun(95) = 91A video solution is available for this question — log in and enroll to watch it.