Jump to content

[SOLVED] Fraction Simplest Form... Anyone?


shamuraq

Recommended Posts

For larger numerator/denominator values, this might be a little slow. But most cases it should be fine:

<?php
  function gcd($num1, $num2) {
    while ($num2 != 0){
      $t = $num1 % $num2;
      $num1 = $num2;
      $num2 = $t;
    }
    return $num1;
  }

  $a = rand(1,20);
  $b = rand(20,30);

  printf('Before: %d/%d<br />',$a,$b);
  $gcd = gcd($a,$b);
  printf('After: %d/%d<br />',$a/$gcd,$b/$gcd);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.