Recursion

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

Recursion

Recursion is a programming technique that can accomplish a lot with a little bit of code.  Because recursive procedures call themselves, their operation seems like magic.   Here are a couple of recursive functions - one to calculate  factorials and one to calculate Fibonacci numbers. 

Example 1: Factorials                    

N factorial for a positive integer N is defined as the product of the integers 1 through N.  It is usually written as N!.   

Function factorial(N:integer):Integer;

Begin

   If N=1 then result:=1 

   else result:=N*factorial(N-1);

end; 

Example 2: Fibonacci Numbers

Fibonacci numbers are members of an infinite series of integers in which the first two numbers are 1, and each number after the 2nd is the sum of the two preceding numbers (1, 1, 2, 3, 5, 8, 13....). Here's a function that returns the Nth Fibonacci number:

  

Function Fibonacci(n:integer) :integer;

Begin

   If N<=2 then result:=1

  else  result:=Fibonacci(n-1)+Fibonacci(n-2);

end;

 

Here is a program that calculates integer powers of a real number.  Recursion is one of 4 techniques tested.  The program will also introduce you to techniques for accurately measuring program run times.  Click here to download source code for IntPowerDemo.

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.