Permalink
Browse files

Add gmp_perfect_power()

Exposes the mpz_perfect_power_p() function.

We already had the more specific gmp_perfect_square() function.
  • Loading branch information...
nikic committed Dec 9, 2017
1 parent a1d36a1 commit 10a336d3d062fdfa0133b22fb4f79852ec939ea5
Showing with 64 additions and 0 deletions.
  1. +1 −0 NEWS
  2. +1 −0 UPGRADING
  3. +20 −0 ext/gmp/gmp.c
  4. +1 −0 ext/gmp/php_gmp.h
  5. +41 −0 ext/gmp/tests/gmp_perfect_power.phpt
1 NEWS
@@ -50,6 +50,7 @@ PHP NEWS
. Export internal structures and accessor helpers for GMP object. (Sara)
. Added gmp_binomial(n, k). (Nikita)
. Added gmp_lcm(a, b). (Nikita)
. Added gmp_perfect_power(a). (Nikita)
- intl:
. Fixed bug #75317 (UConverter::setDestinationEncoding changes source instead
@@ -88,6 +88,7 @@ Date:
GMP:
. Added gmp_binomial(n, k) for calculating binomial coefficients.
. Added gmp_lcm(a, b) for calculating the least common multiple.
. Added gmp_perfect_power(a) to check if number is a perfect power.
Intl:
. Added void Spoofchecker::setRestrictionLevel(int $level) method, available
@@ -164,6 +164,7 @@ const zend_function_entry gmp_functions[] = {
ZEND_FE(gmp_pow, arginfo_gmp_pow)
ZEND_FE(gmp_powm, arginfo_gmp_powm)
ZEND_FE(gmp_perfect_square, arginfo_gmp_unary)
ZEND_FE(gmp_perfect_power, arginfo_gmp_unary)
ZEND_FE(gmp_prob_prime, arginfo_gmp_prob_prime)
ZEND_FE(gmp_gcd, arginfo_gmp_binary)
ZEND_FE(gmp_gcdext, arginfo_gmp_binary)
@@ -1652,6 +1653,25 @@ ZEND_FUNCTION(gmp_perfect_square)
}
/* }}} */
/* {{{ proto bool gmp_perfect_power(mixed a)
Checks if a is a perfect power */
ZEND_FUNCTION(gmp_perfect_power)
{
zval *a_arg;
mpz_ptr gmpnum_a;
gmp_temp_t temp_a;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &a_arg) == FAILURE){
return;
}
FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
RETVAL_BOOL((mpz_perfect_power_p(gmpnum_a) != 0));
FREE_GMP_TEMP(temp_a);
}
/* }}} */
/* {{{ proto int gmp_prob_prime(mixed a[, int reps])
Checks if a is "probably prime" */
ZEND_FUNCTION(gmp_prob_prime)
@@ -54,6 +54,7 @@ ZEND_FUNCTION(gmp_rootrem);
ZEND_FUNCTION(gmp_pow);
ZEND_FUNCTION(gmp_powm);
ZEND_FUNCTION(gmp_perfect_square);
ZEND_FUNCTION(gmp_perfect_power);
ZEND_FUNCTION(gmp_prob_prime);
ZEND_FUNCTION(gmp_gcd);
ZEND_FUNCTION(gmp_gcdext);
@@ -0,0 +1,41 @@
--TEST--
gmp_perfect_power(): Check if number is a perfect power
--FILE--
<?php
var_dump(gmp_perfect_power(0));
var_dump(gmp_perfect_power(1));
var_dump(gmp_perfect_power(2));
var_dump(gmp_perfect_power(4));
var_dump(gmp_perfect_power(6));
var_dump(gmp_perfect_power(8));
echo "\n";
var_dump(gmp_perfect_power(-1));
var_dump(gmp_perfect_power(-2));
var_dump(gmp_perfect_power(-4));
var_dump(gmp_perfect_power(-6));
var_dump(gmp_perfect_power(-8));
echo "\n";
$n = gmp_init("7442665456261594668083173595997");
var_dump(gmp_perfect_power($n));
var_dump(gmp_perfect_power($n+1));
?>
--EXPECT--
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

0 comments on commit 10a336d

Please sign in to comment.