This tutorial demonstrates how to calculate the greatest common divisor and factorial numbers by doing basic math with PHP.
First of all let’s remember what factorials and the greatest common divisors mean.
Factorial – The product of all the positive integers from one to a given number. For example: 4 factorial, usually written 4!, is equal to 24 (123*4 = 24).
Greatest common divisor – The largest number that divides evenly into each of a given set of numbers. Also called greatest common factor, highest common factor.
Let’s create the HTML required for this script. We need a form that will contain a dropdown list that we’ll use to select the function, 3 textboxes to input the values and one submit button. Function Factorial Gcd
n value
a value
b value
You can see that the drop down list has an onchange event triggering a function. That functions is a javascript function that hides or shows the appropiate text boxes depending on what functions is selected. There’s a function that’s called onload too; that function sets the default value of the drop down list to Factorial, and hides the text boxes used by the Gcd function. These functions are used to make the page look more user-friendly, and go in the head of the document.
Let’s move on to the PHP code. First of all we’ll create 4 variables used to store the values of the text boxes and the drop down list.
<?php
$n=$_GET[‘n’];
$a=$_GET[‘a’];
$b=$_GET[‘b’];
$function=$_GET[‘function’];
we’ll have to functions: one for calculating the greatest common divisor and one for the factorial number. So, we have to check the value of the “Function” list in order to call the appropiate function. We also want to avoid calling the function when the page loads (refreshes) and the n fields is empty. If it is empty, no function will be called.
if ($function==”Factorial” && $n!=””)
{
factorial($n);
}
else if ($function==”Gcd”)
{
gcd($a,$b);
}
The factorial function takes the $n variable as a parameter, while the gcd takes the $a and $b variables. Now we’ll write the factorial function.
First we have to check what value was entered. It $n is less than 1, we can not calculate $n!.
function factorial($n)
{
if ($n<1)
{
echo “You must enter an integer bigger than 0”;
}
To calculate $n factorial we have to initialize a variable and use it to store the result of the multiplication of the numbers between 1 and $n. We’ll use a FOR loop to go through all the numbers between 1 and $n.
else
{
$nFactorial=1;
for($i=1;$i<$n+1;$i++)
{
$nFactorial*=$i;
}
echo $n.” factorial is: “.$nFactorial;
}
}
Let’s say $n is 3; $nFactorial will be 11, then 12 and finally 2*3=6. 3!=6
Time to write the Gcm function. Like in the factorial function, we first have to check the variables’ values. If a variable is 0 or it has no value, we can’t calculate the greatest common divisor.
function gcd($a,$b)
{
if ($a==0 || $b==0 || $a==”” || $b==””)
{
echo “You must enter integers different from 0”;
}
There are two situations when calculating the greatest common divisor of 2 numbers. The lucky situation is when the numbers are equal; then the gcd is any of the two numbers. That’s our first case.
else
{
if ($a==$b)
{
echo "gcd is ".$a;
}
The other posibility is that the numbers are not equal. To find the gcd we have to devide the two numbers, if the remainder is different from 0 the first number takes the value of the second, the second number takes the value of the reminder, and we divide them again. We keep doing so until the reminder is 0. To do this we’ll use a do while loop.
else
{
do
{
$rest=$a%$b;
$a=$b;
$b=$rest;
}
while($rest!==0);
echo "the gcd is ".$a;
}
}
}
?>
So, that’s it. You can copy the whole code from below:
<html>
<head>
<script type="text/javascript">
function change()
{
if (document.getElementById('function').selectedIndex==0)
{
document.getElementById('factorial').style.display="block";
document.getElementById('gcd').style.display="none";
}
else
{
document.getElementById('factorial').style.display="none";
document.getElementById('gcd').style.display="block";
}
}
function def()
{
document.getElementById('function').selectedIndex=0;
document.getElementById('gcd').style.display="none";
}
</script>
</head>
<body onload="def()">
<form action="phpTemp.php" method="get">
Function <select name="function" id="function" onchange="change()">
<option>Factorial</option>
<option>Gcd</option>
</select><br /><br />
<div id="factorial">
n value <input type="text" name="n" /></div>
<div id="gcd">
a value <input type="text" name="a" /><br />
b value <input type="text" name="b" /></div><br />
<input type="submit" value="submit" />
</form>
<?php
$n=$_GET['n'];
$a=$_GET['a'];
$b=$_GET['b'];
$function=$_GET['function'];
if ($function=="Factorial" && $n!="")
{
factorial($n);
}
else if ($function=="Gcd")
{
gcd($a,$b);
}
function factorial($n)
{
if ($n<1)
{
echo “You must enter an integer bigger than 0”;
}
else
{
$nFactorial=1;
for($i=1;$i<$n+1;$i++)
{
$nFactorial*=$i;
}
echo $n.”! = “.$nFactorial;
}
}
function gcd($a,$b)
{
if ($a==0 || $b==0 || $a==”” || $b==””)
{
echo “You must enter integers different from 0”;
}
else
{
if ($a==$b)
{
echo “gcd is “.$a;
}
else
{
do
{
$rest=$a%$b;
$a=$b;
$b=$rest;
}
while($rest!==0);
echo “the gcd is “.$a;
}
}
}
?>