public function testGMP()
 {
     $math = \Mdanter\Ecc\EccFactory::getAdapter();
     $I_l = "e97a4d6be13f8f5804c0a76080428fc6d51260f74801678c4127045d2640af14";
     $private_key = "142018c66b43a95de58c1cf603446fc0da322bc15fb4df068b844b57c706dd05";
     $n = "115792089237316195423570985008687907852837564279074904382605163141518161494337";
     $gmp_I_l = gmp_init($I_l, 16);
     $gmp_private_key = gmp_init($private_key, 16);
     $gmp_add = gmp_add($gmp_I_l, $gmp_private_key);
     $gmp_add_res = gmp_strval($gmp_add, 10);
     $this->assertEquals("105604983404708440304568772161069255144976878830542744455590282065741265022740", gmp_strval($gmp_I_l));
     $this->assertEquals("9102967069016248707169900673545386030247334423973996501079368232055584775429", gmp_strval($gmp_private_key));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_add));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_div_r($gmp_add, gmp_init($n))));
     $this->assertEquals("-4", gmp_strval(gmp_cmp(0, gmp_div_r($gmp_add, $n))));
     $this->assertEquals("230500039711040884435309657843302549028061777533591645339274813439315011292506", gmp_strval(gmp_add(gmp_init($n), gmp_div_r($gmp_add, gmp_init($n)))));
     $gmp_mod2 = $math->mod($gmp_add_res, $n);
     $this->assertTrue(is_string($gmp_mod2));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2);
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2);
     // when no base is provided both a resource and string work
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_init($gmp_mod2)));
     $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_mod2));
     // when base is provided it fails on HHVM when inputting a string
     $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval(gmp_init($gmp_mod2), 16));
     // $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval($gmp_mod2, 16));
     $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", str_pad(gmp_strval(gmp_init($gmp_mod2), 16), 64, '0', STR_PAD_LEFT));
 }
Ejemplo n.º 2
0
function fact($number)
{
    $t = $number;
    if (gmp_div_r((string) $t, 2) == 0) {
        $n = 0;
        $n += 1;
        $t /= 2;
        while (gmp_div_r((string) $t, 2) == 0) {
            $n += 1;
            $t /= 2;
        }
        echo "2, {$n}\n";
        if ($t == 1) {
            return;
        }
    }
    $limit = floor(sqrt($t));
    for ($i = 3; $i <= $limit; $i += 2) {
        if (gmp_div_r((string) $t, $i) == 0) {
            $n = 0;
            $n += 1;
            $t /= $i;
            while (gmp_div_r((string) $t, $i) == 0) {
                $n += 1;
                $t /= $i;
            }
            echo "{$i}, {$n}\n";
            if ($t == 1) {
                return;
            }
            $limit = floor(sqrt($t));
        }
    }
    echo "{$t}, 1\n";
}
Ejemplo n.º 3
0
 /**
  * Returns the amount of hundredth's in this decimal fraction part. That would be cents for,
  * say, an USD amount.
  *
  * @return integer
  */
 public function fractionValue()
 {
     $ret = gmp_div_r(gmp_abs(gmp_init($this->cents, 10)), 100);
     if (gmp_cmp(gmp_init($this->cents, 10), 0) < 0) {
         $ret = gmp_neg($ret);
     }
     return gmp_intval($ret);
 }
Ejemplo n.º 4
0
 public static function gmp_mod2($n, $d)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         $res = gmp_div_r($n, $d);
         if (gmp_cmp(0, $res) > 0) {
             $res = gmp_add($d, $res);
         }
         return gmp_strval($res);
     } else {
         throw new Exception("PLEASE INSTALL GMP");
     }
 }
Ejemplo n.º 5
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;
 }
function fatorar_raiz_quadrada($numOrig)
{
    $result = '';
    $fatoracao = fatoracao_numeros_primos($numOrig);
    if (is_array($fatoracao) && count($fatoracao) > 0) {
        $resultFatNumero = 0;
        $resultFatRaiz = array();
        foreach ($fatoracao as $fator => $qtd) {
            if ($qtd > 1) {
                $agrupamentoPares = gmp_div_q($qtd, 2);
                // divide por dois, simples, retorna inteiro
                $resultPares = $fator * gmp_strval($agrupamentoPares);
                // os pares são tirados da raiz quadrada, portanto numeros normais no resultado;
                $resultFatNumero = $resultFatNumero > 0 ? $resultFatNumero * $resultPares : $resultPares;
                $restoImpar = gmp_div_r($qtd, 2);
                if ($restoImpar > 0) {
                    $resultFatRaiz[] = "raiz quadrada(" . gmp_strval($fator) . ")";
                }
            } else {
                $resultFatRaiz[] = "raiz quadrada({$fator})";
            }
        }
        if ($resultFatNumero > 0) {
            $result = $resultFatNumero;
        }
        if (count($resultFatRaiz) > 0) {
            $resultFatRaiz = implode(' * ', $resultFatRaiz);
            if ($result != '') {
                // caso exista numerico, multiplicar por este
                $result .= ' * ';
            }
            $result .= $resultFatRaiz;
        }
    } else {
        $result = "<b>{$fatoracao}<b><br>";
    }
    return $result;
}
Ejemplo n.º 7
0
<?php

var_dump(gmp_div_r());
var_dump(gmp_div_r(""));
var_dump($r = gmp_div_r(0, 1));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1, 0));
var_dump($r = gmp_div_r(12653, 23482734));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(12653, 23482734, 10));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123, 1));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123, 2));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123, GMP_ROUND_ZERO));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123, GMP_ROUND_PLUSINF));
var_dump(gmp_strval($r));
var_dump($r = gmp_div_r(1123123, 123, GMP_ROUND_MINUSINF));
var_dump(gmp_strval($r));
$fp = fopen(__FILE__, 'r');
var_dump(gmp_div_r($fp, $fp));
var_dump(gmp_div_r(array(), array()));
echo "Done\n";
Ejemplo n.º 8
0
echo gmp_strval($div1) . "\n";
$div2 = gmp_div_q("1", "3");
echo gmp_strval($div2) . "\n";
$div3 = gmp_div_q("1", "3", GMP_ROUND_PLUSINF);
echo gmp_strval($div3) . "\n";
$div4 = gmp_div_q("-1", "4", GMP_ROUND_PLUSINF);
echo gmp_strval($div4) . "\n";
$div5 = gmp_div_q("-1", "4", GMP_ROUND_MINUSINF);
echo gmp_strval($div5) . "\n";
// gmp_div_qr
$a = gmp_init("0x41682179fbf5");
$res = gmp_div_qr($a, "0xDEFE75");
var_dump($res);
printf("Result is: q - %s, r - %s" . PHP_EOL, gmp_strval($res[0]), gmp_strval($res[1]));
// gmp_div_r
$div = gmp_div_r("105", "20");
echo gmp_strval($div) . "\n";
// gmp_div
$div1 = gmp_div("100", "5");
echo gmp_strval($div1) . "\n";
// gmp_divexact
$div1 = gmp_divexact("10", "2");
echo gmp_strval($div1) . "\n";
// gmp_fact
$fact1 = gmp_fact(5);
// 5 * 4 * 3 * 2 * 1
echo gmp_strval($fact1) . "\n";
$fact2 = gmp_fact(50);
// 50 * 49 * 48, ... etc
echo gmp_strval($fact2) . "\n";
// gmp_gcd
Ejemplo n.º 9
0
 /**
  * This method evaluates whether the operand is an odd number.
  *
  * @access public
  * @static
  * @param IInteger\Type $x                                  the object to be evaluated
  * @return IBool\Type                                       whether the operand is an odd
  *                                                          number
  */
 public static function isOdd(IInteger\Type $x) : IBool\Type
 {
     return IBool\Type::box(gmp_strval(gmp_div_r($x->unbox(), '2')) != '0');
 }
Ejemplo n.º 10
0
 /**
  * Validate if a value is divisible by an available bill
  * @param unknown $value
  */
 public function validateBillByBill($value)
 {
     foreach ($this->availableBills as $bill) {
         if (gmp_div_r($value, $bill) === 0) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 11
0
function dots($num)
{
    if (strstr($num, ".")) {
        $ost1 = substr($num, 1 + strpos($num, "."));
        $num = substr($num, 0, strpos($num, "."));
    }
    if (substr($num, 0, 1) == "-") {
        $min = 1;
        $num = substr($num, 1);
    }
    if (strstr($num, ".")) {
        $num = substr($num, 0, strpos($num, "."));
    }
    $ost = gmp_strval(gmp_div_r(strlen($num), 3));
    for ($i = 1; $i <= gmp_strval(gmp_div_q(strlen($num), 3)); $i++) {
        $str = substr($num, strlen($num) - $i * 3, 3) . "." . $str;
    }
    if ($ost > 0) {
        $str = substr($num, 0, $ost) . "." . $str;
    }
    $str = substr($str, 0, strlen($str) - 1);
    if ($str == "") {
        $str = 0;
    }
    if ($min) {
        $str = "-" . $str;
    }
    if ($ost1) {
        $str = $str . "," . $ost1;
    }
    return $str;
}
Ejemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function divR($left, $right)
 {
     return gmp_strval(gmp_div_r($left, $right));
 }
Ejemplo n.º 13
0
<?php

echo (double) (600851475143.0 % 3) . "\n";
echo 600851475143.0 % 5 . "\n";
echo 600851475143.0 % 7 . "\n";
echo 600851475143.0 % 9 . "\n";
echo gmp_div_r('600851475143', 3) . "\n";
Ejemplo n.º 14
0
 /**
  * {@inheritdoc}
  */
 public function divR($a, $b)
 {
     return gmp_strval(gmp_div_r($a, $b));
 }
Ejemplo n.º 15
0
                 $l_desc = $row["l_desc"];
                 $my_id = $row["id"];
                 $my_count = $row["my_count"];
                 echo "<td><center>( {$my_count} )<br>{$s_desc}<br><IMG title='{$l_desc}' src='./pics/" . $my_cat_id . "_{$my_id}" . ".jpg'><br></center>";
                 echo "<center><a href='{$cgi}?act=trans&my_id={$my_id}&add=in'><IMG title='Add' src='add.png'></a>";
                 if ($my_count > 0) {
                     echo "<a href='{$cgi}?act=trans&my_id={$my_id}&add=out'><IMG title='Remove' src='minus.png'></a>";
                 } else {
                     //								echo "<IMG title='No inventory to check out' src='nono.png'>";
                 }
                 echo "<a href='{$cgi}?act=del&my_id={$my_id}&add=in'><IMG title='Delete from Inventory' src='delete.png'></a>";
                 echo "<br>";
                 echo "<a href='{$cgi}?act=modify&my_id={$my_id}'><IMG title='Edit' src='edit.png'></a>";
                 echo "<a href='{$cgi}?act=upload&my_id={$my_id}'><IMG title='Change Photo' src='picture.png'></a>";
                 echo "<a href='{$cgi}?act=activity&my_id={$my_id}'><IMG title='History' src='list_edit.png'></center></a></td>\n\n\n";
                 if (gmp_div_r($color, $num_col) == $num_col - 1) {
                     echo "</tr>\n";
                 }
                 $color = $color + 1;
             }
         } else {
             echo "<p><h1>No items</h1></p>";
         }
     }
     echo "</table></center><br><br>";
     NavBar($cgi, $act, $usr_access, $copyrite);
     break;
 case "login":
     shortbanner("Login", $dblink, $css_include_file, $title);
     if (!isset($_SESSION['user_id'])) {
         // do nothing