<?php
// factorial.php
// RJM Programming - August, 2024
// Thanks to https://www.php.net/manual/en/gmp.examples.php

$whereplace=shell_exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'");
if (strpos(($whereplace . ' ' . $_SERVER['SERVER_ADDR']), '65.254.92.213') !== false) {
     header('Location: https://65.254.95.247/PHP/factorial.php'); 
     exit; 
}

$fstyle="";
$pstyle="";
$facttodo=1000;
$pow="1";
if (isset($_GET['facttodo'])) {
 if (strlen('' . $_GET['facttodo']) > 0) {
  $facttodo=str_replace('+',' ',urldecode($_GET['facttodo']));
 }
} else if (isset($_POST['facttodo'])) {
 if (strlen('' . $_POST['facttodo']) > 0) {
  $facttodo=str_replace('+',' ',urldecode($_POST['facttodo']));
 }
}
if (isset($_GET['pow'])) {
 if (strlen('' . $_GET['pow']) > 0) {
  $pow=str_replace('+',' ',urldecode($_GET['pow']));
 }
} else if (isset($_POST['pow'])) {
 if (strlen('' . $_POST['pow']) > 0) {
  $pow=str_replace('+',' ',urldecode($_POST['pow']));
 }
}

function fact($x) {
    $return = 1;
    for ($i=2; $i <= $x; $i++) {
        $return = gmp_mul($return, $i);
    }
    return $return;
}

function thepow($x, $y) {
    return gmp_pow($x, $y);
}

function thegcd($x, $y) {
    return gmp_gcd($x, $y);
}

function thelcm($x, $y) {
    return gmp_lcm($x, $y);
}

function nextprime($x) {
    return gmp_nextprime($x);
}

echo "<html>
<head>
<style>
  form {
    display: inline-block;
  }
  
  textarea {
    background-color: yellow;
  }
</style>
</head>
<body>
<h1>Factorials via GMP</h1>
<h3>RJM Programming - August, 2024</h3>
<h4>Factorial of <form method=POST action=./factorial.php><input style=display:inline-block; onblur=\"document.getElementById('mysub').click();\" type=number step=1 name=facttodo id=facttodo value='" . $facttodo . "'></input><input type=submit style=display:none; id=mysub></input></form> is ...</h4>
<textarea style=width:95%;height:34%;>" . gmp_strval(fact($facttodo)) . "</textarea>
<h4><span>Number </span><form method=POST action=./factorial.php><input style=display:inline-block; data-onblur=\"document.getElementById('mysub').click();\" type=number step=1 name=facttodo id=facttodotwo value='" . $facttodo . "'></input><span> to the power of </span><input style=display:inline-block; onblur=\"document.getElementById('mysubpow').click();\" type=number step=1 name=pow id=pow value='" . $pow . "'></input><input type=submit style=display:none; id=mysubpow></input></form><span> is ...</span></h4>
<textarea style=width:95%;height:5%;>" . gmp_strval(thepow($facttodo, $pow)) . "</textarea>
<h4><span>Next prime greater than </span><form method=POST action=./factorial.php><input style=display:inline-block; onblur=\"document.getElementById('mysubthree').click();\" type=number step=1 name=facttodo id=facttodothree value='" . $facttodo . "'></input><input type=submit style=display:none; id=mysubthree></input></form><span> is ...</span></h4>
<textarea style=width:95%;height:3%;>" . gmp_strval(nextprime($facttodo)) . "</textarea>
<table cellpadding=15>
<tr><td>
<h4><span>Least common multiple of </span><form method=POST action=./factorial.php><input style=display:inline-block; data-onblur=\"document.getElementById('mysub').click();\" type=number step=1 name=facttodo id=facttodofive value='" . $facttodo . "'></input><span> and </span><input style=display:inline-block; onblur=\"document.getElementById('mysubfive').click();\" type=number step=1 name=pow id=powfive value='" . $pow . "'></input><input type=submit style=display:none; id=mysubfive></input></form><span> is ...</span></h4>
<textarea style=width:95%;height:3%;>" . gmp_strval(thegcd($facttodo, $pow)) . "</textarea>
</td><td>
<h4><span>Greatest common divisor of </span><form method=POST action=./factorial.php><input style=display:inline-block; data-onblur=\"document.getElementById('mysub').click();\" type=number step=1 name=facttodo id=facttodofour value='" . $facttodo . "'></input><span> and </span><input style=display:inline-block; onblur=\"document.getElementById('mysubfour').click();\" type=number step=1 name=pow id=powfour value='" . $pow . "'></input><input type=submit style=display:none; id=mysubfour></input></form><span> is ...</span></h4>
<textarea style=width:95%;height:3%;>" . gmp_strval(thelcm($facttodo, $pow)) . "</textarea>
</td></tr></table>
</body></html>";
?>