PHP and GMP tutorial: How to calculate Euler's reduced power of large numbers
Euler's totient function is a common function in number theory, used to calculate a positive integer less than or equal to The number of numbers n that are relatively prime to n. When calculating the Euler power reduction of large numbers, due to the large amount of data, we cannot directly use ordinary calculation methods, but need to use the GMP (GNU Multiple Precision) extension of PHP to perform the operation. This article will introduce how to use PHP and GMP to calculate the Euler reduced power of large numbers and provide code examples.
First, check the PHP extension directory. You can view the current PHP configuration information by executing the phpinfo() function. Find "extension_dir" in the displayed configuration information and record the path of the extension directory.
Next, download the source code of the GMP library from the official GMP website (https://gmplib.org/) and extract it locally.
Open the command line window and enter the decompressed GMP directory.
Execute the following commands to compile and install:
$ ./configure $ make $ make install
After completing the installation, copy the compiled GMP extension file (usually gmp.so or gmp.dll) to the previously recorded extension in the directory.
Edit the php.ini file and add the following lines at the end of the file:
extension=gmp
Save and close the php.ini file.
Restart the web server for the new GMP extension to take effect.
<?php function euler_power($base, $exponent, $modulus) { $result = gmp_init(1); while (gmp_cmp($exponent, 0) > 0) { if (gmp_even($exponent)) { $base = gmp_powm($base, 2, $modulus); $exponent = gmp_div_q($exponent, 2); } else { $result = gmp_mul($result, $base); $exponent = gmp_sub($exponent, 1); } } return gmp_mod($result, $modulus); } // 示例用法 $base = gmp_init(23456789); $exponent = gmp_init(98765432); $modulus = gmp_init(1234567891); $result = euler_power($base, $exponent, $modulus); echo gmp_strval($result); ?>
In the above example code, we define a function named euler_power to calculate Euler's reduced power. The function accepts three parameters: base, exponent and modulus. The function uses loops and conditional judgments to determine the parity of the index, performs corresponding operations based on the parity, and finally returns the calculation result.
In the example usage, we convert the base, exponent and modulus to the integer type of GMP through the gmp_init function. Then call the euler_power function to calculate Euler's reduced power, and use the gmp_strval function to convert the calculation result into a string for output.
Note: When using the GMP function, the type of the parameter must be the integer type of GMP, otherwise an error will occur. Therefore, when defining a variable, you need to use the gmp_init function to convert it to the integer type of GMP.
The above is the detailed content of PHP and GMP Tutorial: How to Calculate the Euler Reduction of Large Numbers. For more information, please follow other related articles on the PHP Chinese website!