Question about recursion
Published by: **Tom Daly** on 11 Feb 2014 view comments(1)

I came across this recently and thought it was pretty funny. The original was in C.

Q: Does this function halt or not halt?

P function B

D function PI 10u 0

D input 10u 0

If input = 0;

return input;

Else;

return function(input - 1);

EndIf;

P E

A1: Mathematician - It only halts for non-negative inputs. Negative inputs will loop infinitely as x approaches minus infinity.

A2: Computer scientist - It always halts due to the eventual stack overflow on a real machine.

A3: Computer scientist - It always halts due to the eventual integer wrap around on a real machine.

A4: Physicist - It always halts because running it infinitely would require more energy than exists in the universe.

Silly

(Sign in to Post a Comment)

Comment on: Question about recursion

Posted: 9 years 9 months 17 days 17 hours 20 minutes ago

Posted: 9 years 9 months 17 days 17 hours 20 minutes ago

I don't think it will compile. You're passing a formula to a parm not defined as Value or Const.

A1: It will halt when the negative number won't fit into 10u0.

Chris Ringer