PHP GNU Multiple Precision Mathematics Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

PHP GNU Multiple Precision Mathematics Tutorial

PHP GNU Multiple Precision Mathematics Tutorial

Maths fun โ€œPHP styleโ€ can be had with the GNU Multiple Precision (GMP) extension with echoes of PHP mathematics work talked about with PHP Bcmath Factorial Tutorial.

Again, on this AlmaLinux PHP (starting with 8) environment, we started off with those ever fascinating โ€ฆ

  • factorial calculations โ€ฆ and then followed that up with โ€ฆ
  • number to the power of โ€ฆ
  • next greatest prime number โ€ฆ
  • least common multiple โ€ฆ
  • greatest common divisor

โ€ฆ functionalities the user can try for themselves โ€ฆ

<?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>";


?>

To install on AlmaLinux we ended up with a โ€œdnfโ€ package manager scenario looking like โ€ฆ



[root@65-254-95-247 ~]# dnf list *gmp*

Last metadata expiration check: 3:23:48 ago on Sat 31 Aug 2024 10:51:31 AM AWST.

Installed Packages

cpanel-perl-536-math-bigint-gmp.x86_64 1.6011-1.cp108~el9 @System

ea-php80-php-gmp.x86_64 8.0.30-4.4.8.cpanel @EA4-c9

ea-php81-php-gmp.x86_64 8.1.29-1.2.1.cpanel @EA4-c9

ea-php82-php-gmp.x86_64 8.2.22-1.2.1.cpanel @EA4-c9

gmp.x86_64 1:6.2.0-13.el9 @baseos

Available Packages

alt-php-internal-gmp.x86_64 8.2.14-2.el9 imunify360

alt-php82-gmp.x86_64 8.2.8-1.el9 imunify360

ea-php80-php-gmp-debuginfo.x86_64 8.0.28-1.1.29.cpanel EA4-c9

ea-php81-php-gmp-debuginfo.x86_64 8.1.19-2.2.1.cpanel EA4-c9

ea-php82-php-gmp-debuginfo.x86_64 8.2.6-2.2.1.cpanel EA4-c9

ea-php83-php-gmp.x86_64 8.3.10-1.2.1.cpanel EA4-c9

gmp.i686 1:6.2.0-13.el9 baseos

gmp-c++.i686 1:6.2.0-13.el9 appstream

gmp-c++.x86_64 1:6.2.0-13.el9 appstream

gmp-devel.i686 1:6.2.0-13.el9 appstream

gmp-devel.x86_64 1:6.2.0-13.el9 appstream

perl-Crypt-DH-GMP.x86_64 0.00012-24.el9 epel

perl-Math-BigInt-GMP.x86_64 1.6007-1.el9 epel

perl-Math-GMP.x86_64 2.24-1.el9 epel

python-gmpy2-doc.noarch 2.1.2-4.el9 epel

python3-gmpy2.x86_64 2.1.2-4.el9 epel

[root@65-254-95-247 ~]#

โ€ฆ for a functional firstโšชdraft โ€œproof of conceptโ€ GMP based mathematics web application the user can try for themselves below โ€ฆ



Previous relevant PHP Bcmath Factorial Tutorial is shown below.

PHP Bcmath Factorial Tutorial

PHP Bcmath Factorial Tutorial

Today weโ€™ve had great guidance from this webpageโ€™s contributors, thanks, writing a Factorial Calculator, using PHP and its Bcmath extension, revisiting concepts we last talked about when we presented PHP Bcmath Primer Tutorial.

Are you like me with maps and sometimes with long numbers โ€ฆ just staring at a long integer trying to find patterns?

Well, you need to let โ€ฆ

Factorials

โ€ฆ into your life โ€ฆ the bigger the better! Wonder at all those zeroes at the end? Carve a long factorial into a piece of soap? They say the Lordโ€™s Prayer was written on a pinhead โ€ฆ what are you waiting for?!

Or just avail yourself of todayโ€™s use_bcmathโšซphp first draft PHP Factorial Calculator โ€ฆ

โ€ฆ therapy.


Previous relevant PHP Bcmath Primer Tutorial is shown below.

PHP Bcmath Primer Tutorial

PHP Bcmath Primer Tutorial

Today we have an unusual linkage of tutorials, where we link our tutorial here, today, called โ€œPHP Bcmath Primer Tutorialโ€ with one from some time back (and I.T. historically speaking, a really long way back) called Lisp Primer Tutorial as shown below.

The link is the way the languages handle some arithmetic on very large numbers, and today we present a web application that is like a calculator for some of those very large number calculations. Am not certain Lisp does the work this way, but the โ€œbcmathโ€ module in PHP, that we use for the purpose here today, uses โ€œstringsโ€ to achieve the affect, so that I donโ€™t believe there are any times during the calculations, necessarily, with the CPU register, that there are any great huge numbers being handled, but piece by piece, the CPU register is helping piece together that string that represents the answer we are after, not using any scientific notation in the process.

We throw into the mix a bit of fun and games โ€ฆ just imagine you are on Central (train) Station in Sydney, or loads of other train stations in the world where it is pretty obvious the station announcements are pieced together bits of audio, and today we make use of the wonderful Google Translate functionality in order for you to, optionally (and not always โ€ฆ no go with Firefox for instance), hear the results in those great mellifluous tones (in English, as default).

Hope you really give some curly questions out there to the liveโœ‚run. Here is the PHP source code using the โ€œbcmathโ€ module you could call got_big_numbersโšซphp


Previous relevant Lisp Primer Tutorial is shown below.

Lisp Primer Tutorial

Lisp Primer Tutorial

Have you heard of Lisp, the programming language? Do you remember how incredible Hewlett Packard calculators were back in the 1970โ€™s (we did as Land Surveyors โ€ฆ they were the beeโ€™s knees, especially to program for those Land Surveying calculations). And do you (older users) remember the excitement regarding reverse Polish notation? Well, Lisp, the second oldest high-level language (after Fortran) uses this reverse Polish notation and thinking.

Am no expert with Lisp, as youโ€™ll see, and weโ€™ll defer to code available on the net and contributed by others, but have to mention its incredible mathematical scope, for instance, with its functionality for huge numbers (integers). You will notice with languages like C++ it is not at all easy doing a calculation like the factorial of 1000 โ€ฆ or 1000! โ€ฆ or 1000 x 999 x 998 x โ€”> x 2 x 1 โ€ฆ but Lisp does it with considerable ease. As a matter of interest, as far as this type of big integer in Java, you may want to check on the BigO data type.

Todayโ€™s tutorial introduces you to Lisp we install using LispWorks Personal on a Mac laptop, and execute a Hello World and some large factorial calculations. Lisp, being the old language it is, is a very functional language, and although global variables are allowed, the Lisp โ€œwayโ€ is not to resort to such approaches.

Below are some links regarding Lisp you may want to peruse:

Will leave you with some downloadable programming Lisp source code you could call helloworldโšซlisp and some factorial code called factorialโšซlisp

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Installers, Not Categorised, Tutorials and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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