How To Find The GCD Of Two Integers In PHP

The gcd of two integers is the largest integer that divides evenly into both integers with no remainder. This article describes how to calculate the gcd of two integers using PHP using a browser.

Calculate The GCD OF Two Integers With User Input Through A Browser.

Getting user input in PHP through forms is slightly cumbersome, but I will walk you though what you need to do.

Here are the steps required:

  • Step 1. Make a PHP function that calculates gcd
  • Step 2. Have an HTML file or PHP file that has a form that a user will submit data to another PHP form which uses the function  we made in step 1.
  • Step 3. Make the file that gets the user’s post data in step 2 and calculates the gcd of those two integers using the function we made in step 1, and then output the gcd to the user.

Step 1. Make a PHP function that calculates gcd

Below is some code that makes a function called gcd, and it will calculate and return the gcd of two integers given to it.

<?php function gcd($x,$y) { // if $y > $x, then switch them
  if ($y > $x) {
    $temp1 = $y;
    $y = $x;
    $x = $temp1;
  }

  // edge cases. If x or y are negative, if one or both of them is a 0, or if they are the same number.
  if ($x == 0 && $y == 0) return 1;
  if ($x == 0) return $y;
  if ($y == 0) return $x;
  if ($x < 0) $x = -$x;
  if ($y < 0) $y = -$y; if ($x == $y) return $x; // Using the Euclidean algorithm with x being the divisor and y being the remainder for each step after the first step while ($y > 0) {
    $temp2 = $y;
    $y = $x % $y;
    $x = $temp2;
  }
  
  // return the divisor when the remainder is 0
  return $x;
}

?>

You can download this php code that calculates the gcd of two numbers here. You can run the above code as is, if you use php interactively with php -a or php --interactive and test it out there. But likely,  you’ll want to run that code through a browser, so you’ll need to submit the numbers through an HTML form. We will create the file with a form next.

Step 2: Create a file with a form, that the user will submit the two integers through

As mentioned, the above code will work, but you will either need to include it in another PHP file, or run it in a php shell. But, I will assume we are going to make a file with a form to submit 2 integers to calculate the gcd of. See below for the form code.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    

This program will calculate the gcd of your given 2 integers.

    
<form action="calculate_gcd.php" method="post">
      Enter your first integer  <input type="text" name="integer1" />
      Enter your second integer <input type="text" name="integer2" />
      <input type="submit" name="submit" value="Calculate!" />
    </form>

  </body>
</html>

I am calling the above form code, get_data.php, but you can name it whatever you want. Likewise, I am calling the file that will generate the code calculate_gcd.php, which I will make in the next step, but if you don’t want to call it calculate_gcd.php, edit as appropriate.

You can download the above code here.

Step 3. Get the post data from step 2, calculate the gcd using step 1, and then return to the user the gcd

The first step is to include the function we made in step 1. We can do this with include 'gcd.php';. Then, we need to get the POST data the user sends in step 2. Can can do this with $_POST['integer1'] and $_POST['integer2']. integer1 and integer2 were the names of the inputs I made in the form. You can use other names if you like, just edit where appropriate. Next, we simply use the function gcd to calculate gcd of the two integers the user sent, and then show the user that result. See the code below.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Get GCD of two integers PHP</title>
  </head>
  <body>
  <?php include 'gcd.php';
  $first_integer = $_POST['integer1'];
  $second_integer= $_POST['integer2'];
  $calculated_gcd = gcd($first_integer, $second_integer);

  echo "the gcd of " . $first_integer . " and " . $second_integer . " is " . $calculated_gcd;
  echo "
Calculate again? <a href='get_data.php'>go back </a>";
  ?>
  </body>
</html>

You can download calculate_gcd.php here.

Final Notes

Make sure that you put the three files gcd.php, get_data.php, and calculate_gcd.php in the same folder. In addition, make sure that you have a web server like Apache installed, that you are serving the files from the web server, if using Apache make sure php mod for your server installed and enabled, and PHP itself installed on your server. Currently, PHP is in version 7.1. If you use a shared web host, it is very likely your host has already installed and configured PHP, so you probably don’t have to worry about it. You will only need to make sure it is installed if you run your own server or use a VPS.

Want to know how to calculate the gcd of two integers in other languages? See how to do it in Javascript, and how to do it in Python.

What did you think of this article? Have anything to add? Let’s discuss it in the comments below.

Posted on Categories Math, PHP

2 thoughts on “How To Find The GCD Of Two Integers In PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *