Midrange News for the IBM i Community

Posted by: Tom Daly
Question about recursion
has no ratings.
Published: 11 Feb 2014
Revised: 13 Feb 2014 - 3578 days ago
Last viewed on: 30 Nov 2023 (5617 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

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;
     return function(input - 1);

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.


Return to midrangenews.com home page.
Sort Ascend | Descend


(Sign in to Post a Comment)
Posted by: Ringer
Premium member *
Comment on: Question about recursion
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