本文整理汇总了PHP中phpseclib\Math\BigInteger::copy方法的典型用法代码示例。如果您正苦于以下问题:PHP BigInteger::copy方法的具体用法?PHP BigInteger::copy怎么用?PHP BigInteger::copy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类phpseclib\Math\BigInteger
的用法示例。
在下文中一共展示了BigInteger::copy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: extendedGCD
/**
* Calculates the greatest common divisor and Bezout's identity.
*
* Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that
* 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which
* combination is returned is dependant upon which mode is in use. See
* {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.
*
* Here's an example:
* <code>
* <?php
* $a = new \phpseclib\Math\BigInteger(693);
* $b = new \phpseclib\Math\BigInteger(609);
*
* extract($a->extendedGCD($b));
*
* echo $gcd->toString() . "\r\n"; // outputs 21
* echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21
* ?>
* </code>
*
* @param \phpseclib\Math\BigInteger $n
* @return \phpseclib\Math\BigInteger
* @access public
* @internal Calculates the GCD using the binary xGCD algorithim described in
* {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes,
* the more traditional algorithim requires "relatively costly multiple-precision divisions".
*/
function extendedGCD($n)
{
switch (MATH_BIGINTEGER_MODE) {
case self::MODE_GMP:
extract(gmp_gcdext($this->value, $n->value));
return array('gcd' => $this->_normalize(new static($g)), 'x' => $this->_normalize(new static($s)), 'y' => $this->_normalize(new static($t)));
case self::MODE_BCMATH:
// it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works
// best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is,
// the basic extended euclidean algorithim is what we're using.
$u = $this->value;
$v = $n->value;
$a = '1';
$b = '0';
$c = '0';
$d = '1';
while (bccomp($v, '0', 0) != 0) {
$q = bcdiv($u, $v, 0);
$temp = $u;
$u = $v;
$v = bcsub($temp, bcmul($v, $q, 0), 0);
$temp = $a;
$a = $c;
$c = bcsub($temp, bcmul($a, $q, 0), 0);
$temp = $b;
$b = $d;
$d = bcsub($temp, bcmul($b, $q, 0), 0);
}
return array('gcd' => $this->_normalize(new static($u)), 'x' => $this->_normalize(new static($a)), 'y' => $this->_normalize(new static($b)));
}
$y = $n->copy();
$x = $this->copy();
$g = new static();
$g->value = array(1);
while (!($x->value[0] & 1 || $y->value[0] & 1)) {
$x->_rshift(1);
$y->_rshift(1);
$g->_lshift(1);
}
$u = $x->copy();
$v = $y->copy();
$a = new static();
$b = new static();
$c = new static();
$d = new static();
$a->value = $d->value = $g->value = array(1);
$b->value = $c->value = array();
while (!empty($u->value)) {
while (!($u->value[0] & 1)) {
$u->_rshift(1);
if (!empty($a->value) && $a->value[0] & 1 || !empty($b->value) && $b->value[0] & 1) {
$a = $a->add($y);
$b = $b->subtract($x);
}
$a->_rshift(1);
$b->_rshift(1);
}
while (!($v->value[0] & 1)) {
$v->_rshift(1);
if (!empty($d->value) && $d->value[0] & 1 || !empty($c->value) && $c->value[0] & 1) {
$c = $c->add($y);
$d = $d->subtract($x);
}
$c->_rshift(1);
$d->_rshift(1);
}
if ($u->compare($v) >= 0) {
$u = $u->subtract($v);
$a = $a->subtract($c);
$b = $b->subtract($d);
} else {
$v = $v->subtract($u);
//.........这里部分代码省略.........