Skip to content

Commit

Permalink
Add gmp_lcm()
Browse files Browse the repository at this point in the history
Exposes mpz_lcm() and mpz_lcm_ui() for calculating the least
common multiple.

We already expose the somewhat complementary gmp_gcd() function.
  • Loading branch information
nikic committed Dec 11, 2017
1 parent 7fea796 commit a1d36a1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ext/gmp/php_gmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions ext/gmp/tests/gmp_lcm.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
gmp_lcm(): Least common multiple
--FILE--
<?php

var_dump(gmp_lcm(100, 77));
var_dump(gmp_lcm(99, 77));
var_dump(gmp_lcm(99, -77));
var_dump(gmp_lcm(-99, -77));

var_dump(gmp_lcm(gmp_init(99), gmp_init(77)));

var_dump(gmp_lcm(93, 0));
var_dump(gmp_lcm(0, 93));

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

0 comments on commit a1d36a1

Please sign in to comment.