Fast Multiplication of Large Integers Using PHP GMP Library

This article explains the fundamentals of large integer multiplication, introduces the efficient GMP library in PHP, describes the fast multiplication algorithm that reduces complexity from O(n²) to O(n log n), and provides a complete PHP code example implementing the method.

php Courses
php Courses
php Courses
Fast Multiplication of Large Integers Using PHP GMP Library

In computer science, integer operations are fundamental, but traditional methods become inefficient for very large integers; this article shows how to use PHP's GMP (GNU Multiple Precision) library to perform fast multiplication of big numbers.

1. Introduction to GMP Library

The GMP library provides high‑precision arithmetic, supporting addition, subtraction, multiplication, division, and exponentiation for arbitrarily large integers. PHP includes a GMP extension that wraps these capabilities with a simple interface.

2. Fast Multiplication Algorithm

The fast multiplication algorithm reduces the multiplication complexity from O(n²) to O(n log n) by applying a divide‑and‑conquer strategy that splits each operand into high‑ and low‑order parts, recursively computes three sub‑products, and combines them using a Karatsuba‑like formula.

The process involves: (1) splitting the two large numbers x and y into high (a, c) and low (b, d) halves; (2) recursively computing the products a·c, b·d, and (a+b)·(c+d); (3) deriving the middle term (a+b)(c+d) − ac − bd; and (4) assembling the final result with appropriate powers of ten.

3. PHP Code Example

The following PHP code implements the described algorithm using the GMP extension.

<?php<br/><span style="color: rgb(198, 120, 221); line-height: 26px">function</span> multiply(<span style="color: rgb(209, 154, 102); line-height: 26px">$x</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$y</span>) {<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$x_gmp</span> = gmp_init(<span style="color: rgb(209, 154, 102); line-height: 26px">$x</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$y_gmp</span> = gmp_init(<span style="color: rgb(209, 154, 102); line-height: 26px">$y</span>);<br/>    <br/>    // 当待乘数小于等于一个阈值时,直接返回乘法结果<br/>    <span style="color: rgb(198, 120, 221); line-height: 26px">if</span> (gmp_cmp(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_gmp</span>, <span style="color: rgb(152, 195, 121); line-height: 26px">"1000000"</span>) <= 0 || gmp_cmp(<span style="color: rgb(209, 154, 102); line-height: 26px">$y_gmp</span>, <span style="color: rgb(152, 195, 121); line-height: 26px">"1000000"</span>) <= 0) {<br/>        <span style="color: rgb(230, 192, 123); line-height: 26px">return</span> gmp_strval(gmp_mul(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_gmp</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$y_gmp</span>));<br/>    }<br/>    <br/>    // 将待乘数分解为高位部分<span style="color: rgb(209, 154, 102); line-height: 26px">$a</span>$和低位部分<span style="color: rgb(209, 154, 102); line-height: 26px">$b</span>$<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$x_str</span> = gmp_strval(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_gmp</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span> = ceil(strlen(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_str</span>) / 2);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$a</span> = substr(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_str</span>, 0, -<span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$b</span> = substr(<span style="color: rgb(209, 154, 102); line-height: 26px">$x_str</span>, -<span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>);<br/>    <br/>    // 将乘数对应分解为高位部分<span style="color: rgb(209, 154, 102); line-height: 26px">$c</span>$和低位部分<span style="color: rgb(209, 154, 102); line-height: 26px">$d</span>$<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$y_str</span> = gmp_strval(<span style="color: rgb(209, 154, 102); line-height: 26px">$y_gmp</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$c</span> = substr(<span style="color: rgb(209, 154, 102); line-height: 26px">$y_str</span>, 0, -<span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$d</span> = substr(<span style="color: rgb(209, 154, 102); line-height: 26px">$y_str</span>, -<span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>);<br/>    <br/>    // 计算子问题的结果<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$ac</span> = multiply(<span style="color: rgb(209, 154, 102); line-height: 26px">$a</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$c</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$bd</span> = multiply(<span style="color: rgb(209, 154, 102); line-height: 26px">$b</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$d</span>);<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$abcd</span> = multiply(gmp_add(<span style="color: rgb(209, 154, 102); line-height: 26px">$a</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$b</span>), gmp_add(<span style="color: rgb(209, 154, 102); line-height: 26px">$c</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$d</span>));<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$ad_bc</span> = gmp_sub(<span style="color: rgb(209, 154, 102); line-height: 26px">$abcd</span>, gmp_add(<span style="color: rgb(209, 154, 102); line-height: 26px">$ac</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$bd</span>));<br/>    <br/>    // 计算最终结果并返回<br/>    <span style="color: rgb(209, 154, 102); line-height: 26px">$result</span> = gmp_add(gmp_mul(gmp_pow(10, 2 * <span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>), <span style="color: rgb(209, 154, 102); line-height: 26px">$ac</span>), gmp_add(gmp_mul(gmp_pow(10, <span style="color: rgb(209, 154, 102); line-height: 26px">$split_point</span>), <span style="color: rgb(209, 154, 102); line-height: 26px">$ad_bc</span>), <span style="color: rgb(209, 154, 102); line-height: 26px">$bd</span>));<br/>    <span style="color: rgb(230, 192, 123); line-height: 26px">return</span> gmp_strval(<span style="color: rgb(209, 154, 102); line-height: 26px">$result</span>);<br/>}<br/><br/>// 示例输入<br/><span style="color: rgb(209, 154, 102); line-height: 26px">$x</span> = <span style="color: rgb(152, 195, 121); line-height: 26px">"12345678901234567890"</span>;<br/><span style="color: rgb(209, 154, 102); line-height: 26px">$y</span> = <span style="color: rgb(152, 195, 121); line-height: 26px">"98765432109876543210"</span>;<br/><br/>// 调用乘法函数<br/><span style="color: rgb(209, 154, 102); line-height: 26px">$result</span> = multiply(<span style="color: rgb(209, 154, 102); line-height: 26px">$x</span>, <span style="color: rgb(209, 154, 102); line-height: 26px">$y</span>);<br/><span style="color: rgb(230, 192, 123); line-height: 26px">echo</span> <span style="color: rgb(152, 195, 121); line-height: 26px">"Result: "</span> . <span style="color: rgb(209, 154, 102); line-height: 26px">$result</span> . <span style="color: rgb(152, 195, 121); line-height: 26px">"<br/>"</span>;<br/>?>

Running the example with two 20‑digit numbers demonstrates the fast multiplication of large integers.

Conclusion

The article presented how to leverage PHP's GMP library and a fast multiplication algorithm to lower the computational complexity of big‑integer multiplication, improving efficiency for applications that require high‑precision arithmetic.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GMPbig integer
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.