Esempio n. 1
0
 /**
  * @return integer Or string, if the number is too big.
  */
 public function centsValue()
 {
     if (gmp_cmp(gmp_abs(gmp_init($this->cents, 10)), PHP_INT_MAX) > 0) {
         return $this->cents;
     }
     return gmp_intval(gmp_init($this->cents, 10));
 }
Esempio n. 2
0
function sumT($n)
{
    if (gmp_intval($n) == 1) {
        return gmp_init(1);
    }
    return gmp_add(gmp_sub(gmp_pow($n, 2), gmp_pow(gmp_init(gmp_intval($n) - 1), 2)), sumT(gmp_init(gmp_intval($n) - 1)));
    //return gmp_mod(gmp_add(gmp_sub(gmp_mod(gmp_pow($n, 2), '1000000007'), gmp_mod(gmp_pow(gmp_init(gmp_intval($n)-1), 2), '1000000007')), sumT(gmp_init(gmp_intval($n)-1))), '1000000007');
}
Esempio n. 3
0
 public function getResult($num)
 {
     // init
     $prime = 1;
     for ($i = 0; $i < $num; $i++) {
         $prime = gmp_nextprime($prime);
     }
     return gmp_intval($prime);
 }
Esempio n. 4
0
 /**
  * This will return either the integer value of the number or the string value
  * (if the integer value is too large). Use this when you prefer to receive
  * an integer, but you also don't want the number to be rounded down to the
  * maximum integer size if it is too large.
  *
  * @return string || int
  */
 public function GetValue()
 {
     $comparison = gmp_cmp($this->number, $this->int_max);
     if ($comparison > 0) {
         return gmp_strval($this->number);
     } else {
         return gmp_intval($this->number);
     }
 }
Esempio n. 5
0
 function irand($min, $max = null)
 {
     if (is_null($max)) {
         $max = $min;
         $min = 0;
     }
     $this->next();
     return gmp_intval(gmp_mod(gmp_init('0x' . $this->context), gmp_init($max - $min))) + $min;
 }
 /**
  * {@inheritdoc}
  */
 public function encodeSFixed64($sFixed64)
 {
     $value = $this->is32Bit ? gmp_and($sFixed64, '0x0ffffffffffffffff') : gmp_init(sprintf('%u', $sFixed64));
     $bytes = '';
     for ($i = 0; $i < 8; ++$i) {
         $bytes .= chr(gmp_intval(gmp_and($value, $this->gmp_xff)));
         $value = gmp_div_q($value, $this->gmp_x100);
     }
     return $bytes;
 }
Esempio n. 7
0
 /**
  * @dataProvider providerTestTAGLong
  */
 public function testPutTAGLong($value)
 {
     $fPtr = fopen($this->vFile->url(), 'wb');
     // 32-bit longs seem to be too long for pack() on 32-bit machines. Split into 4x16-bit instead.
     $quarters[0] = gmp_div(gmp_and($value, '0xFFFF000000000000'), gmp_pow(2, 48));
     $quarters[1] = gmp_div(gmp_and($value, '0x0000FFFF00000000'), gmp_pow(2, 32));
     $quarters[2] = gmp_div(gmp_and($value, '0x00000000FFFF0000'), gmp_pow(2, 16));
     $quarters[3] = gmp_and($value, '0xFFFF');
     $binary = pack('nnnn', gmp_intval($quarters[0]), gmp_intval($quarters[1]), gmp_intval($quarters[2]), gmp_intval($quarters[3]));
     $this->dataHandler->putTAGLong($fPtr, $value);
     $this->assertSame($binary, $this->vFile->getContent());
 }
Esempio n. 8
0
 /**
  * Decode an integer.
  *
  * @param  int $value
  * @return int
  */
 public function decode($value)
 {
     if (!is_numeric($value)) {
         throw new InvalidArgumentException('Argument should be an integer');
     }
     switch (static::$mode) {
         case static::MODE_GMP:
             return gmp_intval(gmp_mul((int) $value ^ $this->xor, $this->inverse)) & static::MAX_INT;
         default:
             return ((int) $value ^ $this->xor) * $this->inverse & static::MAX_INT;
     }
 }
Esempio n. 9
0
 public static function getGCD($numberA, $numberB)
 {
     if (!self::isInteger($numberA) | !self::isInteger($numberB)) {
         throw new \InvalidArgumentException("GCD number must be an integer");
     }
     if (function_exists("gmp_gcd")) {
         return gmp_intval(gmp_gcd($numberA, $numberB));
     }
     if ($numberA == 0 || $numberB == 0) {
         return max(abs($numberA), abs($numberB));
     }
     $r = $numberA % $numberB;
     return $r != 0 ? self::getGCD($numberB, $r) : abs($numberB);
 }
Esempio n. 10
0
function base58_encode($string)
{
    $table = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
    $long_value = gmp_init(bin2hex($string), 16);
    $result = '';
    while (gmp_cmp($long_value, 58) > 0) {
        list($long_value, $mod) = gmp_div_qr($long_value, 58);
        $result .= $table[gmp_intval($mod)];
    }
    $result .= $table[gmp_intval($long_value)];
    for ($nPad = 0; $string[$nPad] == ""; ++$nPad) {
    }
    return str_repeat($table[0], $nPad) . strrev($result);
}
 public function convertFractions(array $number)
 {
     $target = gmp_init($this->target->getRadix());
     $dividend = $this->getDecimal($number);
     $divisor = $this->getDecimal([$this->source->getDigit(1)] + array_fill(1, max(count($number), 1), $this->source->getDigit(0)));
     $digits = $this->getFractionDigitCount(count($number));
     $zero = gmp_init('0');
     $result = [];
     for ($i = 0; $i < $digits && gmp_cmp($dividend, $zero) > 0; $i++) {
         list($digit, $dividend) = gmp_div_qr(gmp_mul($dividend, $target), $divisor);
         $result[] = gmp_intval($digit);
     }
     return $this->target->getDigits(empty($result) ? [0] : $result);
 }
Esempio n. 12
0
 public function totalPages()
 {
     $remainder = gmp_div_r(count($this->items_array), $this->items_x_page);
     $quotient = gmp_intval(gmp_div_q(count($this->items_array), $this->items_x_page));
     if ($quotient > 0) {
         if ($remainder > 0) {
             $this->total_pages = $quotient + 1;
         } else {
             $this->total_pages = $quotient;
         }
     } else {
         $this->total_pages = 1;
     }
     return $this->total_pages;
 }
Esempio n. 13
0
 /**
  * @param int $nominator
  * @param int $denominator
  */
 public function __construct($nominator, $denominator = 1)
 {
     if ($nominator == 0) {
         $denominator = 1;
     } else {
         if (function_exists('gmp_gcd')) {
             $gcd = gmp_intval(gmp_gcd((string) $nominator, (string) $denominator));
             $nominator /= $gcd;
             $denominator /= $gcd;
         } else {
             if ($nominator / $denominator == (int) ($nominator / $denominator)) {
                 $nominator = $nominator / $denominator;
                 $denominator = 1;
             }
         }
     }
     $this->denominator = $denominator;
     $this->nominator = $nominator;
 }
 /**
  * Encode a binary string to base58
  *
  * @param $binary
  * @return string
  * @throws \Exception
  */
 protected function encodeBase58($binary_bitcoin_address)
 {
     $size = strlen($binary_bitcoin_address);
     if ($size == 0) {
         return '';
     }
     $orig = $binary_bitcoin_address;
     $decimal = gmp_import($binary_bitcoin_address);
     $return = "";
     while (gmp_cmp($decimal, 0) > 0) {
         list($decimal, $rem) = gmp_div_qr($decimal, 58);
         $return = $return . self::$BASE_58_CHARS[gmp_intval($rem)];
     }
     $return = strrev($return);
     //leading zeros
     for ($i = 0; $i < $size && $orig[$i] == ""; $i++) {
         $return = "1" . $return;
     }
     return $return;
 }
 public function testConstructInitializeGmpValues()
 {
     $encoder = new GmpNegativeEncoder();
     $gmp_x00 = $this->getPropertyValue($encoder, 'gmp_x00');
     $gmp_x7f = $this->getPropertyValue($encoder, 'gmp_x7f');
     $gmp_x80 = $this->getPropertyValue($encoder, 'gmp_x80');
     $gmp_xff = $this->getPropertyValue($encoder, 'gmp_xff');
     $gmp_x100 = $this->getPropertyValue($encoder, 'gmp_x100');
     $is32Bit = $this->getPropertyValue($encoder, 'is32Bit');
     $this->assertNotNull($gmp_x00);
     $this->assertNotNull($gmp_x7f);
     $this->assertNotNull($gmp_x80);
     $this->assertNotNull($gmp_xff);
     $this->assertNotNull($gmp_x100);
     $this->assertEquals(0, gmp_intval($gmp_x00));
     $this->assertEquals(127, gmp_intval($gmp_x7f));
     $this->assertEquals(128, gmp_intval($gmp_x80));
     $this->assertEquals(255, gmp_intval($gmp_xff));
     $this->assertEquals(256, gmp_intval($gmp_x100));
     $this->assertEquals(BigEndian::is32Bit(), $is32Bit);
 }
Esempio n. 16
0
 public function set($value)
 {
     $size = static::INTEGER_BITS;
     // Versions before PHP 5.6 used resources to represent big numbers
     // while new versions use objects instead.
     if (is_resource($value) && get_resource_type($value) === 'GMP integer' || $value instanceof \GMP) {
         // It is already a GMP integer.
     } else {
         $value = @gmp_init($value, 10);
     }
     if ($value === false) {
         throw new \InvalidArgumentException("Expected a signed {$size}-bits integer value");
     }
     // Check type bounds.
     $binval = gmp_strval($value, 2);
     if (!strncmp($binval, '-1', 2)) {
         $binval = (string) substr($binval, 2);
     }
     if (strlen($binval) >= $size) {
         throw new \InvalidArgumentException("Expected a signed {$size}-bits integer value");
     }
     $this->value = $size <= 32 ? gmp_intval($value) : $value;
 }
Esempio n. 17
0
 protected function next($bits)
 {
     $temp = function_exists('gmp_mul') ? gmp_intval(gmp_mul($this->seed, self::MULTIPLIER)) : self::int_mul($this->seed, self::MULTIPLIER);
     $this->seed = self::mask($temp + self::ADDEND);
     return self::i32(self::rshiftu($this->seed, 48 - $bits));
 }
 public static function gmp_dec2base($dec, $base, $digits = FALSE)
 {
     if (extension_loaded('gmp')) {
         if ($base < 2 or $base > 256) {
             die("Invalid Base: " . $base);
         }
         $value = "";
         if (!$digits) {
             $digits = self::digits($base);
         }
         $dec = gmp_init($dec);
         $base = gmp_init($base);
         while (gmp_cmp($dec, gmp_sub($base, '1')) > 0) {
             $rest = gmp_mod($dec, $base);
             $dec = gmp_div($dec, $base);
             $value = $digits[gmp_intval($rest)] . $value;
         }
         $value = $digits[gmp_intval($dec)] . $value;
         return (string) $value;
     } else {
         throw new ErrorException("Please install GMP");
     }
 }
Esempio n. 19
0
 /**
  *
  * @see Encodable::toDER()
  * @return string
  */
 public function toDER()
 {
     $bytes = array();
     $byte = $this->_class << 6 | $this->_pc << 5;
     $tag = gmp_init($this->_tag, 10);
     if ($tag < 0x1f) {
         $bytes[] = $byte | $tag;
     } else {
         // long-form identifier
         $bytes[] = $byte | 0x1f;
         $octets = array();
         for (; $tag > 0; $tag >>= 7) {
             array_push($octets, gmp_intval(0x80 | $tag & 0x7f));
         }
         // last octet has bit 8 set to zero
         $octets[0] &= 0x7f;
         foreach (array_reverse($octets) as $octet) {
             $bytes[] = $octet;
         }
     }
     return pack("C*", ...$bytes);
 }
Esempio n. 20
0
 public static function GetIntVal($Number)
 {
     return gmp_intval($Number);
 }
Esempio n. 21
0
File: Iban.php Progetto: tpweb/iban
 private function ibanMod97_10($numeric_representation, $useGmp = true)
 {
     // prefer php5 gmp extension if available
     if ($useGmp && function_exists('gmp_intval')) {
         return gmp_intval(gmp_mod(gmp_init($numeric_representation, 10), '97')) === 1;
     }
     $length = strlen($numeric_representation);
     $rest = "";
     $position = 0;
     while ($position < $length) {
         $value = 9 - strlen($rest);
         $n = $rest . substr($numeric_representation, $position, $value);
         $rest = $n % 97;
         $position = $position + $value;
     }
     return $rest === 1;
 }
Esempio n. 22
0
    printf($fmt, gmp_strval($a), gmp_strval($r['s']), gmp_strval($b), gmp_strval($r['t']), gmp_strval($r['g']));
} else {
    echo "Error while solving the equation\n";
}
// gmp_hamdist
$ham1 = gmp_init("1001010011", 2);
$ham2 = gmp_init("1011111100", 2);
echo gmp_hamdist($ham1, $ham2) . "\n";
echo gmp_popcount(gmp_xor($ham1, $ham2)) . "\n";
// gmp_init (although that's probably tested well by now)
var_dump($a = gmp_init(123456));
var_dump($b = gmp_init("0xFFFFDEBACDFEDF7200"));
// gmp_intval
echo gmp_intval(PHP_INT_MAX) . "\n";
echo gmp_intval(gmp_add(PHP_INT_MAX, 1)) . "\n";
echo gmp_intval(gmp_sub(PHP_INT_MAX, 1)) + 1 . "\n";
echo gmp_strval(gmp_add(PHP_INT_MAX, 1)) . "\n";
// gmp_invert
echo gmp_invert("5", "10");
// no inverse, outputs nothing, result is FALSE
$invert = gmp_invert("5", "11");
echo gmp_strval($invert) . "\n";
// gmp_jacobi
echo gmp_jacobi("1", "3") . "\n";
echo gmp_jacobi("2", "3") . "\n";
// gmp_legendre
echo gmp_legendre("1", "3") . "\n";
echo gmp_legendre("2", "3") . "\n";
// gmp_mod
$mod = gmp_mod("8", "3");
echo gmp_strval($mod) . "\n";
 public function parse(DateTimeParseContext $context, $text, $position)
 {
     $length = strlen($text);
     if ($position === $length) {
         return ~$position;
     }
     if ($position < 0 || $position >= $length) {
         throw new \OutOfRangeException();
     }
     $sign = $text[$position];
     $negative = false;
     $positive = false;
     if ($sign === $context->getDecimalStyle()->getPositiveSign()) {
         if ($this->signStyle->parse(true, $context->isStrict(), $this->minWidth === $this->maxWidth) === false) {
             return ~$position;
         }
         $positive = true;
         $position++;
     } else {
         if ($sign === $context->getDecimalStyle()->getNegativeSign()) {
             if ($this->signStyle->parse(false, $context->isStrict(), $this->minWidth === $this->maxWidth) === false) {
                 return ~$position;
             }
             $negative = true;
             $position++;
         } else {
             if ($this->signStyle == SignStyle::ALWAYS() && $context->isStrict()) {
                 return ~$position;
             }
         }
     }
     $effMinWidth = $context->isStrict() || $this->isFixedWidth($context) ? $this->minWidth : 1;
     $minEndPos = $position + $effMinWidth;
     if ($minEndPos > $length) {
         return ~$position;
     }
     $effMaxWidth = ($context->isStrict() || $this->isFixedWidth($context) ? $this->maxWidth : 9) + Math::max($this->subsequentWidth, 0);
     $total = 0;
     $totalBig = null;
     $pos = $position;
     for ($pass = 0; $pass < 2; $pass++) {
         $maxEndPos = Math::min($pos + $effMaxWidth, $length);
         while ($pos < $maxEndPos) {
             $ch = $text[$pos++];
             $digit = $context->getDecimalStyle()->convertToDigit($ch);
             if ($digit < 0) {
                 $pos--;
                 if ($pos < $minEndPos) {
                     return ~$position;
                     // need at least min width digits
                 }
                 break;
             }
             if ($pos - $position > 18) {
                 if ($totalBig === null) {
                     $totalBig = \gmp_init($total);
                 }
                 $totalBig = \gmp_add(\gmp_mul($totalBig, "10"), \gmp_init($digit));
             } else {
                 $total = $total * 10 + $digit;
             }
         }
         if ($this->subsequentWidth > 0 && $pass === 0) {
             // re-parse now we know the correct width
             $parseLen = $pos - $position;
             $effMaxWidth = Math::max($effMinWidth, $parseLen - $this->subsequentWidth);
             $pos = $position;
             $total = 0;
             $totalBig = null;
         } else {
             break;
         }
     }
     if ($negative) {
         if ($totalBig !== null) {
             if (\gmp_cmp($totalBig, "0") === 0 && $context->isStrict()) {
                 return ~($position - 1);
                 // minus zero not allowed
             }
             $totalBig = \gmp_neg($totalBig);
         } else {
             if ($total === 0 && $context->isStrict()) {
                 return ~($position - 1);
                 // minus zero not allowed
             }
             $total = -$total;
         }
     } else {
         if ($this->signStyle == SignStyle::EXCEEDS_PAD() && $context->isStrict()) {
             $parseLen = $pos - $position;
             if ($positive) {
                 if ($parseLen <= $this->minWidth) {
                     return ~($position - 1);
                     // '+' only parsed if minWidth exceeded
                 }
             } else {
                 if ($parseLen > $this->minWidth) {
                     return ~$position;
                     // '+' must be parsed if minWidth exceeded
                 }
             }
         }
     }
     if ($totalBig !== null) {
         if (gmp_cmp($totalBig, "-9223372036854775808") < 0 || gmp_cmp($totalBig, "9223372036854775807") > 0) {
             // overflow, parse 1 less digit
             $totalBig = gmp_div($totalBig, "10");
             $pos--;
         }
         return $this->setValue($context, gmp_intval($totalBig), $position, $pos);
     }
     return $this->setValue($context, $total, $position, $pos);
 }
Esempio n. 24
0
 /**
  * Get the native PHP integer value
  *
  * @param bool $strict
  * @access public
  * @return int
  */
 public function toInteger($strict = true)
 {
     if ($strict && !$this->isWithinIntegerRange()) {
         throw new OutOfTypeRangeException('The value "' . $this->toString() . '" is outside of the native integer range: ' . ~PHP_INT_MAX . '...' . PHP_INT_MAX);
     }
     return gmp_intval($this->getRawValue());
 }
Esempio n. 25
0
$bits = gmp_strval($s, 2);
while (strlen($bits) % 8 != 0) {
    $bits = "0" . $bits;
}
#echo "bits : $bits\n";
$res = "";
$size = strlen($bits) / 8;
$codes = array();
for ($i = 0; $i < $size; $i++) {
    $num = 0;
    $base = 1;
    for ($j = 7; $j >= 0; $j--) {
        $num += $bits[8 * $i + $j] == '1' ? $base : 0;
        $base *= 2;
    }
    $pmod = gmp_intval(gmp_mod($p, "13"));
    $codes[] = ($num + 256 - $pmod) % 256;
}
for ($i = 0; $i < $size; $i++) {
    $ch = mb_convert_encoding(pack("N", $codes[$i]), "UTF-8", "UCS-4BE");
    $res = $res . $ch;
}
$rcvData = explode(" ", $res);
$email = $rcvData[2];
$deviceID = $rcvData[1];
$cdkey = $rcvData[0];
$dbHandle = "";
try {
    $dbHandle = new PDO("mysql:dbname=" . $dbName . ";host=" . $dbHost, $dbUser, $dbPass);
} catch (PDOException $e) {
    die("Decoder : couldn\\'t connect to the DB.\n");
Esempio n. 26
0
<?php

/**
 * Summation of primes
 * 
 * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
 * Find the sum of all the primes below two million.
 */
$primes_below = 2000000;
$primes_sum = 0;
$i = 0;
while ($i < $primes_below) {
    $i = gmp_intval(gmp_nextprime($i));
    if ($i < $primes_below) {
        $primes_sum += $i;
    }
}
echo $primes_sum;
Esempio n. 27
0
 /**
  * Transforms large integer into binary representation.
  *
  * Example of transformation:
  *    $num = 0x9078563412;
  *    $str = "\x12\x34\x56\x78\x90";
  *
  * @param gmp resource $num
  * @return string
  * @access public
  */
 function int2bin($num)
 {
     $result = '';
     do {
         $result .= _byte_chr(gmp_intval(gmp_mod($num, 256)));
         $num = gmp_div($num, 256);
     } while (gmp_cmp($num, 0));
     return $result;
 }
Esempio n. 28
0
 /**
  * Creates an instance of {@code Duration} from a number of seconds.
  *
  * @param $nanos mixed the number of nanoseconds, positive or negative
  * @return Duration a {@code Duration}, not null
  * @throws ArithmeticException if numeric overflow occurs
  */
 private static function createBC($nanos)
 {
     $divRem = gmp_div_qr($nanos, LocalTime::NANOS_PER_SECOND);
     if (gmp_cmp($divRem[0], "-9223372036854775808") < 0 || gmp_cmp($divRem[0], "9223372036854775807") > 0) {
         throw new ArithmeticException("Exceeds capacity of Duration: " . gmp_strval($nanos));
     }
     return self::ofSeconds(gmp_intval($divRem[0]), gmp_intval($divRem[1]));
 }
Esempio n. 29
0
 public function sFixed64_gmp($value)
 {
     static $xff, $x100;
     if (NULL === $xff) {
         $xff = gmp_init(0xff);
         $x100 = gmp_init(0x100);
     }
     $value = PHP_INT_SIZE < 8 ? gmp_and($value, '0x0ffffffffffffffff') : gmp_init(sprintf('%u', $value));
     $bytes = '';
     for ($i = 0; $i < 8; $i++) {
         $bytes .= chr(gmp_intval(gmp_and($value, $xff)));
         $value = gmp_div_q($value, $x100);
     }
     $this->write($bytes);
 }
Esempio n. 30
0
<?php

include "database_connect.php";
$uid = gmp_intval(gmp_random_range(1000000, 9999999));
$username = $_POST['username'];
$email = $_POST['email'];
$password_raw = $_POST['password'];
$password_hashed = password_hash($password_raw, PASSWORD_DEFAULT);
$sql_search_username = "******";
$sql_search_email = "select uid from users where email='{$email}';";
$result1 = $mysqli->query($sql_search_username);
$result2 = $mysqli->query($sql_search_email);
if ($result1->num_rows == 0 && $result2->num_rows == 0) {
    $sql_insert_user = "******";
    if ($mysqli->query($sql_insert_user) === false) {
        echo mysqli_error($mysqli);
        die($sql_insert_user);
    }
    $table_name = 'contact_' . $uid;
    $sql_create_table = 'create table ' . $table_name . ' (
		ID int(11) auto_increment primary key,
		firstname varchar(256) null,
		lastname varchar(256) null,
		gender varchar(256) null,
		phone varchar(256) null,
		phoneRaw varchar(256) null,
		phoneType varchar(256) null,
		addressLineOne varchar(256) null,
		addressLineTwo varchar(256) null,
		city varchar(256) null,
		state varchar(256) null,