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 - 3718 days ago
Last viewed on: 17 Apr 2024 (5807 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;
   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

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

COMMENTS

(Sign in to Post a Comment)
Posted by: Ringer
Premium member *
Comment on: Question about recursion
Posted: 10 years 2 months 7 days 10 hours 5 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