Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Recursion in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Recursion in PHP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    2003-03-08

    Table of Contents:
  • Recursion in PHP
  • Factorials
  • Fibonacci
  • Iterative Fibonacci

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Recursion in PHP - Factorials


    (Page 2 of 4 )

    Factorial is expressed as some number ( n) followed by an exclamation point, like so:

    n!  or 5!

    The function is defined as the product of the number n (must be an integer) multiplied by all positive integers less than that number, so 5! =5*4*3*2*1.

    n! =n*(n-1)*(n-2)*...*1

    This formula is helpful, but it's not recursive. If we compress the last part of the formula though, or take out the 5* part, we notice the remainder is 4*3*2*1 which is the same as 4!. So 5! = 5 * 4!.

    Again we do the same and see 4! = 4*3!. And so on. One interesting note is that 0! =1. 0! = 1 will become an exit condition in our formula later.

    So we define two cases for our function.

    if n = 1 or n = 0, then the function will return 1.
    if n > 1 then return n * (n-1)!

    In PHP this looks like:

    <?php
    function factorial$n )
    {
       if (
    $n == || $n== 1)
      { 
    // $n == 0 or $n == 1 is the exit condition
        
    return 1;
      }
      else
      { 
    // the next line contains our recursive call
         
    return ( $n factorial ($n-1));
      }
    }
    ?>

    Next thing to do is make sure it passes the rules above. The function has to have an exit condition. It does. We know the function value explicitly whenever $n is 0 or 1 and the function returns 1 whenever this condition is met, so we have an exit condition. Secondly, it must make different function calls. We know the initial call was factorial( $n ) and the call it makes in the function is to factorial( $n-1), so it passes this test. Lastly, we have to believe it works. I do. If you were to trace this function for factorial (5) you would see something like:

    factorial (5) Ă’ return 5 *
    factorial (4) -> return 4 *
    factorial (3) -> return 3 *
    factorial (2) -> return 2 *
    factorial (1) -> return 1

    The function would finally return 120 as the result of factorial (5).

    More Miscellaneous Articles
    More By Codewalkers


       · I know its nit-picking, but the example code above is a perfect example of recursive...
       · Oh. Comments by tutorial rather than by page. The code I am referring to in the...
       · You are correct. It also doesn't check to see that you are passing in a integer or...
       · The code for factorials is not significantly simpler than safe code, just lazier. ...
       · There is an error in listing of last Procedure. In line: " for ($i = 1; $i <...
       · <?phpfunction f($c) { if ($c<2) throw new Exception('Input out of range'); $f...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek