From a1d36a1157bd88afd64119be059812dd46c4fb2d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 9 Dec 2017 21:22:37 +0100 Subject: [PATCH] Add gmp_lcm() Exposes mpz_lcm() and mpz_lcm_ui() for calculating the least common multiple. We already expose the somewhat complementary gmp_gcd() function. --- NEWS | 1 + UPGRADING | 1 + ext/gmp/gmp.c | 9 ++++++++ ext/gmp/php_gmp.h | 1 + ext/gmp/tests/gmp_lcm.phpt | 45 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 ext/gmp/tests/gmp_lcm.phpt diff --git a/NEWS b/NEWS index cde7a3656d..ddc07a5605 100644 --- a/NEWS +++ b/NEWS @@ -49,6 +49,7 @@ PHP NEWS - GMP: . Export internal structures and accessor helpers for GMP object. (Sara) . Added gmp_binomial(n, k). (Nikita) + . Added gmp_lcm(a, b). (Nikita) - intl: . Fixed bug #75317 (UConverter::setDestinationEncoding changes source instead diff --git a/UPGRADING b/UPGRADING index a68d318074..1560e5913f 100644 --- a/UPGRADING +++ b/UPGRADING @@ -87,6 +87,7 @@ Date: GMP: . Added gmp_binomial(n, k) for calculating binomial coefficients. + . Added gmp_lcm(a, b) for calculating the least common multiple. Intl: . Added void Spoofchecker::setRestrictionLevel(int $level) method, available diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index 667df7cdad..225b2ca034 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -167,6 +167,7 @@ const zend_function_entry gmp_functions[] = { ZEND_FE(gmp_prob_prime, arginfo_gmp_prob_prime) ZEND_FE(gmp_gcd, arginfo_gmp_binary) ZEND_FE(gmp_gcdext, arginfo_gmp_binary) + ZEND_FE(gmp_lcm, arginfo_gmp_binary) ZEND_FE(gmp_invert, arginfo_gmp_binary) ZEND_FE(gmp_jacobi, arginfo_gmp_binary) ZEND_FE(gmp_legendre, arginfo_gmp_binary) @@ -1679,6 +1680,14 @@ ZEND_FUNCTION(gmp_gcd) } /* }}} */ +/* {{{ proto GMP gmp_lcm(mixed a, mixed b) + Computes least common multiple (lcm) of a and b */ +ZEND_FUNCTION(gmp_lcm) +{ + gmp_binary_ui_op(mpz_lcm, (gmp_binary_ui_op_t) mpz_lcm_ui); +} +/* }}} */ + /* {{{ proto array gmp_gcdext(mixed a, mixed b) Computes G, S, and T, such that AS + BT = G = `gcd' (A, B) */ ZEND_FUNCTION(gmp_gcdext) diff --git a/ext/gmp/php_gmp.h b/ext/gmp/php_gmp.h index 8ba4b89d5b..2ee7879e82 100644 --- a/ext/gmp/php_gmp.h +++ b/ext/gmp/php_gmp.h @@ -79,6 +79,7 @@ ZEND_FUNCTION(gmp_popcount); ZEND_FUNCTION(gmp_hamdist); ZEND_FUNCTION(gmp_nextprime); ZEND_FUNCTION(gmp_binomial); +ZEND_FUNCTION(gmp_lcm); ZEND_BEGIN_MODULE_GLOBALS(gmp) zend_bool rand_initialized; diff --git a/ext/gmp/tests/gmp_lcm.phpt b/ext/gmp/tests/gmp_lcm.phpt new file mode 100644 index 0000000000..1534b3ff3a --- /dev/null +++ b/ext/gmp/tests/gmp_lcm.phpt @@ -0,0 +1,45 @@ +--TEST-- +gmp_lcm(): Least common multiple +--FILE-- + +--EXPECT-- +object(GMP)#1 (1) { + ["num"]=> + string(4) "7700" +} +object(GMP)#1 (1) { + ["num"]=> + string(3) "693" +} +object(GMP)#1 (1) { + ["num"]=> + string(3) "693" +} +object(GMP)#1 (1) { + ["num"]=> + string(3) "693" +} +object(GMP)#3 (1) { + ["num"]=> + string(3) "693" +} +object(GMP)#3 (1) { + ["num"]=> + string(1) "0" +} +object(GMP)#3 (1) { + ["num"]=> + string(1) "0" +} -- 2.40.0