Zend - The PHP Company




Math

Add Code


Greatest Common Denominator  

Type: code fragment
Added by: zak
Entered: 09/06/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 6355
A simple class that finds the greatest common denominator for two integers.


<?
function GCD ($a$b)
{
    while ( 
$b != 0)
      {
        
$remainder $a $b;
        
$a $b;
        
$b $remainder;
      }
    return 
abs ($a);
}

/*
DESCRIPTION
============================================================
GCD:
A simple function that finds the Greatest Common Denominator 
for two numbers.
============================================================

CREDITS
============================================================
Author: J. A. Greant ( zak@nucleus.com )
Version 1: June 9, 2000
============================================================

NOTICES
============================================================
If you:
    Modify this code and want to share your changes :)
    Find a bug
    Have other questions or comments

Please write:
    Zak Greant (zak@nucleus.com)
============================================================

USAGE EXAMPLES
============================================================
print GCD (12, 120); // Should output 12

============================================================

COPYRIGHT
============================================================
Copyright (c) 2000 J. A. Greant ( zak@nucleus.com )
All rights reserved.

This function is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any
later version.

This function is distributed in the hope that it will be
useful,but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General
Public License along with this function; if not, write to
the:
    Free Software Foundation, Inc.
    59 Temple Place, Suite 330
    Boston, MA
    02111-1307 USA
    http://www.gnu.org/
============================================================
*/
?>


Usage Example


print GCD (12, 40);


Rate This Script





Search



This Category All Categories