The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+16 votes
2.2k views

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;
  1. $89$
  2. $90$
  3. $91$
  4. $92$
asked in Algorithms by Veteran (59.5k points)
edited by | 2.2k views
+2
Unless you tell what is starting point , how can we tell where recursion stops ?
0
Yes elemenation condition is not there l. But we can take help of options

2 Answers

+25 votes
Best answer
Value returned by $\text{fun}(95) = \text{fun}(\text{fun}(106))$
$\qquad\qquad= \text{fun}(96)$
$\qquad\qquad= \text{fun}(\text{fun}(107))$
$\qquad\qquad= \text{fun}(97)$
$\qquad\qquad= \text{fun}(\text{fun}(108))$
$\qquad\qquad= \text{fun}(98)$
$\qquad\qquad = \text{fun}(\text{fun}(109))$
$\qquad\qquad= \text{fun}(99)$
$\qquad\qquad= \text{fun}(\text{fun}(110))$
$\qquad\qquad= \text{fun}(100) $
$\qquad\qquad= \text{fun}(\text{fun}(111)) $
$\qquad\qquad= \text{fun}(101) = 91.$
answered by Veteran (55.1k points)
edited by
+1

 f(95) 
= f(f(106) = f(96) 
= f(f(107) = f(97) 
= f(f(108) = f(108) 
= f(f(109) = f(109) 
= f(f(110) = f(100) 
= f(f(111) = f(101) = 91

0
@Digvijay what is happening at fun : x-10.
0
thats a typo- corrected..
0
How did we get when to stop the recursion ?
+4 votes
Yes 91 is correct.

step 1: when fun=111 -> fun(fun(111))

step 2:  fun=101 ->fun(101)

step 3:As 101>100 fun=fun -10 =101-10=91

step 4: as there is no fun function call remaining ,it will exit.
answered by (401 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

37,939 questions
45,453 answers
131,190 comments
48,206 users