Author Topic: PHP codes to simplify fractions  (Read 870 times)

Offline msu_math

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
PHP codes to simplify fractions
« on: November 15, 2012, 10:30:38 AM »
The FracSymp($A) function returns the simplest form of the fraction $A. 

function FracSymp($A)

  {
    if(FracCheck($A)==0 || FracDenom($A)==0) { return NULL; }   

    if(FracNumer($A)%FracDenom($A)==0) 

            $ret=FracNumer($A)/FracDenom($A)."";

    elseif(FracDenom($A)%FracNumer($A)==0)

           $ret="1"."/".FracDenom($A)/FracNumer($A);

    else
            {
                 $gcd = NumGCD(FracNumer($A), FracDenom($A));
                 $ret = FracNumer($A)/$gcd."/".FracDenom($A)/$gcd;
            }

    if(FracDenom($ret)<0) 

            { $ret = -FracNumer($ret)."/".-FracDenom($ret); }

    return $ret;
  }
« Last Edit: November 16, 2012, 06:15:59 PM by msu_math »
Mohammad Salah Uddin

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU

Offline msu_math

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: PHP codes to simplify a fraction
« Reply #1 on: November 15, 2012, 10:33:49 AM »
The NumGCD() function returns the greatest common divisor(GCD) of two numbers only and the function NumNGCD() returns GCD of several numbers.

function NumGCD($A, $B)
 {
   if($A==0 || $B==0) { return NULL;}
   if($A<0)  $A=-$A;
   if($B<0)  $B=-$B;
   if($A>=$B) { $bigger=$A; $smaller=$B; }
   else { $bigger=$B; $smaller=$A; }
   while(1)
      {
         $C = $bigger%$smaller; if($C==0) { $gcd=$smaller.""; return $gcd; }
         $bigger = $smaller; $smaller = $C;
     }
 }

function NumNGCD($A)
  {
    $ngcd=$A[1]; $i=1;
    while($A[$i]!=NULL) { $ngcd=NumGCD($ngcd,$A[$i]); $i++; }
    return $ngcd;
  }

Mohammad Salah Uddin

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU

Offline msu_math

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: PHP codes to simplify a fraction
« Reply #2 on: November 15, 2012, 10:35:25 AM »
NumLCM() function returns least common multiple(LCM) of two numbers only and the function NumNLCM() returns LCM of several numbers.   

function NumLCM($A, $B)
 {
   if($A<0) { $A = -$A; }
   if($B<0) { $B = -$B; }
   if($A>=$B) { $bigger=$A; }
   else  { $bigger=$B; }
   for($n=$bigger;$n<$A*$B;$n+=$bigger)
       {
            if($n%$A==0 && $n%$B==0) { $lcm=$n.""; return $lcm; }
       }
   $lcm=$A*$B."";  return $lcm;   
 }

function NumNLCM($A)
  {
    $nlcm=$A[1]; $i=1;
    while($A[$i] != NULL) { $nlcm=NumLCM($nlcm,$A[$i]); $i++; }
    return $nlcm;
  }

Mohammad Salah Uddin

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU

Offline msu_math

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: PHP codes to simplify fractions
« Reply #3 on: November 19, 2012, 11:34:23 AM »
Sample return values of FracSymp() function:

FracSymp("24/32");       Return value:  "3/4"

FracSymp("-21/7");        Return value:  "-3"

FracSymp("1/8");           Return value:  "1/8"

FracSymp("22/1");         Return value:  "22"
Mohammad Salah Uddin

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU