• Welcome to Daffodil International University.
 

News:

Daffodil International University Forum contains information about Open Text material, which is only intended for the significant learning purposes of the university students, faculty, other members, and the knowledge seekers of the entire world and is hoped that the offerings will aide in the distribution of reliable information and data relating to the many areas of knowledge.

Main Menu

PHP codes to simplify fractions

Started by msu_math, November 15, 2012, 10:30:38 AM

Previous topic - Next topic

msu_math

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;
  }
Mohammad Salah Uddin

Lecturer in Mathematics
Department of Natural Sciences
FSIT, DIU

msu_math

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

msu_math

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

msu_math

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