function convBase($numberInput, $fromBaseInput, $toBaseInput) { if ($fromBaseInput == $toBaseInput) { return $numberInput; } $fromBase = str_split($fromBaseInput, 1); $toBase = str_split($toBaseInput, 1); $number = str_split($numberInput, 1); $fromLen = strlen($fromBaseInput); $toLen = strlen($toBaseInput); $numberLen = strlen($numberInput); $retval = ''; $base10 = ''; if ($toBaseInput == '0123456789') { $retval = 0; for ($i = 1; $i <= $numberLen; $i++) { $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); } return $retval; } if ($fromBaseInput != '0123456789') { $base10 = convBase($numberInput, $fromBaseInput, '0123456789'); } else { $base10 = $numberInput; } if ($base10 < strlen($toBaseInput)) { return $toBase[$base10]; } while ($base10 != '0') { $retval = $toBase[bcmod($base10, $toLen)] . $retval; $base10 = bcdiv($base10, $toLen, 0); } return $retval; }
/** * Triples by picolll * * x=0 y=z dla j>2 * x >= 0 * y >= x * z >= y * m >= z * 100 >= m >=5 * 2 <= n <= 100 * */ function solve($m, $n) { $sum = 0; for ($j = 2; $j <= $n; $j++) { if ($j > 2) { $sum += $m + 1; continue; } for ($x = 0; $x <= $m; $x++) { for ($y = $x; $y <= $m; $y++) { if ($x == 0) { $sum++; continue; } $x2 = bcpow($x, $j); $y2 = bcpow($y, $j); $x2y2 = bcadd($x2, $y2); if ($x2y2 == 0) { $sum++; continue; } for ($z = $y; $z <= $m; $z++) { $z2 = bcpow($z, $j); if ($x2y2 == $z2) { $sum++; if ($x != 0) { } continue; } } } } } return $sum; }
function square_chain($n, &$total, &$x) { $p = $n; while ($p > 1 && $p != 89) { $digits = str_split($p); $sum = 0; foreach ($digits as $digit) { $sum += bcpow($digit, 2); } $p = $sum; if ($n > 567 && in_array($p, $x)) { $total++; //$x[]=$n; //echo "in array $p for number $n<br />"; return; } //echo "N is $n and P is $p <br />"; } switch ($p) { case 1: break; case 89: $total++; $x[] = $n; return $total; break; default: echo "error!"; break; } //print_r($x); }
/** * Exponential expression / raise number into power * * @param string $base base to raise * @param string $exp exponent to use * @param mixed $use_function pow function to use, or false for auto-detect * * @return mixed string or float */ function PMA_pow($base, $exp, $use_function = false) { static $pow_function = null; if (null == $pow_function) { $pow_function = PMA_detect_pow(); } if (!$use_function) { $use_function = $pow_function; } if ($exp < 0 && 'pow' != $use_function) { return false; } switch ($use_function) { case 'bcpow': // bcscale() needed for testing PMA_pow() with base values < 1 bcscale(10); $pow = bcpow($base, $exp); break; case 'gmp_pow': $pow = gmp_strval(gmp_pow($base, $exp)); break; case 'pow': $base = (double) $base; $exp = (int) $exp; $pow = pow($base, $exp); break; default: $pow = $use_function($base, $exp); } return $pow; }
/** * Exponential expression / raise number into power * * @param string $base base to raise * @param string $exp exponent to use * @param string $use_function pow function to use, or false for auto-detect * * @return mixed string or float */ public static function pow($base, $exp, $use_function = '') { static $pow_function = null; if ($pow_function == null) { $pow_function = self::detectPow(); } if (!$use_function) { if ($exp < 0) { $use_function = 'pow'; } else { $use_function = $pow_function; } } if ($exp < 0 && $use_function != 'pow') { return false; } switch ($use_function) { case 'bcpow': // bcscale() needed for testing pow() with base values < 1 bcscale(10); $pow = bcpow($base, $exp); break; case 'gmp_pow': $pow = gmp_strval(gmp_pow($base, $exp)); break; case 'pow': $base = (double) $base; $exp = (int) $exp; $pow = pow($base, $exp); break; default: $pow = $use_function($base, $exp); } return $pow; }
private function pow_mod($p, $q, $r) { $factors = array(); $div = $q; $power_of_two = 0; while (bccomp($div, "0") == 1) { $rem = bcmod($div, 2); $div = bcdiv($div, 2); if ($rem) { array_push($factors, $power_of_two); } $power_of_two++; } $partial_results = array(); $part_res = $p; $idx = 0; foreach ($factors as $factor) { while ($idx < $factor) { $part_res = bcpow($part_res, "2"); $part_res = bcmod($part_res, $r); $idx++; } array_push($partial_results, $part_res); } $result = "1"; foreach ($partial_results as $part_res) { $result = bcmul($result, $part_res); $result = bcmod($result, $r); } return $result; }
/** * @see http://php.net/manual/en/function.base-convert.php#106546 * * @param $numberInput * @param $fromBaseInput * @param $toBaseInput * * @return int|string */ protected static function convertBase($numberInput, $fromBaseInput, $toBaseInput) { if ($fromBaseInput == $toBaseInput) { return $numberInput; } $fromBase = str_split($fromBaseInput, 1); $toBase = str_split($toBaseInput, 1); $number = str_split($numberInput, 1); $fromLen = strlen($fromBaseInput); $toLen = strlen($toBaseInput); $numberLen = strlen($numberInput); $retval = ''; if ($toBaseInput == self::FORMAT_NUMBER) { $retval = 0; for ($i = 1; $i <= $numberLen; $i++) { $retval = bcadd($retval, bcmul(array_search($number[$i - 1], $fromBase), bcpow($fromLen, $numberLen - $i))); } return $retval; } if ($fromBaseInput != self::FORMAT_NUMBER) { $base10 = self::convertBase($numberInput, $fromBaseInput, self::FORMAT_NUMBER); } else { $base10 = $numberInput; } if ($base10 < strlen($toBaseInput)) { return $toBase[$base10]; } while ($base10 != '0') { $retval = $toBase[bcmod($base10, $toLen)] . $retval; $base10 = bcdiv($base10, $toLen, 0); } return $retval; }
function alpha_id($in, $to_num = false, $pad_up = 6, $passkey = '', $index = '') { if (!$passkey) { $passkey = "Dj*H&^(#x43)[=Na!"; } if (!$index) { $index = "ABCDEF012345"; } if ($passkey !== null) { for ($n = 0; $n < strlen($index); $n++) { $i[] = substr($index, $n, 1); } $passhash = hash('sha256', $passkey); $passhash = strlen($passhash) < strlen($index) ? hash('sha512', $passkey) : $passhash; for ($n = 0; $n < strlen($index); $n++) { $p[] = substr($passhash, $n, 1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } $base = strlen($index); if ($to_num) { $in = strrev($in); $out = 0; $len = strlen($in) - 1; for ($t = 0; $t <= $len; $t++) { $bcpow = bcpow($base, $len - $t); $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $out -= pow($base, $pad_up); } } $out = sprintf('%F', $out); $out = substr($out, 0, strpos($out, '.')); } else { if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $in += pow($base, $pad_up); } } $out = ""; for ($t = floor(log($in, $base)); $t >= 0; $t--) { $bcp = bcpow($base, $t); $a = floor($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - $a * $bcp; } $out = strrev($out); } if ($to_num === true) { return $out <= PHP_INT_MAX ? intval($out) : (double) $out; } else { return $out; } }
function GetMaxVal() { if (function_exists("bcpow")) { return round(bcpow(10, $this->scale[1], 15), 14); } else { return round(pow(10, $this->scale[1]), 14); } }
private function normalize($numberOfBytes) { $numberOfBytes = (string) $numberOfBytes; if (preg_match('/^(?P<coefficient>\\d+(?:\\.\\d+))E\\+(?P<exponent>\\d+)$/', $numberOfBytes, $matches)) { $numberOfBytes = bcmul($matches['coefficient'], bcpow($base = 10, $matches['exponent'], self::COMPUTE_WITH_PRECISION)); } return $numberOfBytes; }
public static function encode($in, $to_num = false, $pad_up = false, $passKey = null) { $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($passKey !== null) { // Although this function's purpose is to just make the // ID short - and not so much secure, // with this patch by Simon Franz (http://blog.snaky.org/) // you can optionally supply a password to make it harder // to calculate the corresponding numeric ID for ($n = 0; $n < strlen($index); $n++) { $i[] = substr($index, $n, 1); } $passhash = hash('sha256', $passKey); $passhash = strlen($passhash) < strlen($index) ? hash('sha512', $passKey) : $passhash; for ($n = 0; $n < strlen($index); $n++) { $p[] = substr($passhash, $n, 1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } $base = strlen($index); if ($to_num) { // Digital number <<-- alphabet letter code $in = strrev($in); $out = 0; $len = strlen($in) - 1; for ($t = 0; $t <= $len; $t++) { $bcpow = bcpow($base, $len - $t); $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow; } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $out -= pow($base, $pad_up); } } $out = sprintf('%F', $out); $out = substr($out, 0, strpos($out, '.')); } else { // Digital number -->> alphabet letter code if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $in += pow($base, $pad_up); } } $out = ""; for ($t = floor(log($in, $base)); $t >= 0; $t--) { $bcp = bcpow($base, $t); $a = floor($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - $a * $bcp; } $out = strrev($out); // reverse } return $out; }
function q48() { $sum = 0; for ($i = 1; $i <= 1000; ++$i) { $power = bcpow($i, $i); $sum = bcadd($sum, $power); } return substr($sum, -10); }
/** * Converts a long binary number to a unsigned integer as BCMath string. * * @param string $binary a binary number as bit string * @return string the resulting integer as BCMath value */ public static function binaryToInt($binary) { $res = '0'; $n = strlen($binary); for ($i = 0; $i < $n; $i++) { $res = bcadd($res, bcmul($binary[$n - $i - 1], bcpow('2', $i)), 0); } return $res; }
static function decode($code, $codeset, $base) { // $base = strlen($codeset); $c = '0'; for ($i = strlen($code); $i; $i--) { $c = bcadd($c, bcmul(strpos($codeset, substr($code, -1 * ($i - strlen($code)), 1)), bcpow($base, $i - 1))); } return bcmul($c, 1, 0); }
function CalculateTimePerBlock($btc_difficulty, $_hashrate) { if ($btc_difficulty > 0 && $_hashrate > 0) { $find_time_hours = $btc_difficulty * bcpow(2, 32) / ($_hashrate * bcpow(10, 6)) / 3600; } else { $find_time_hours = 0; } return $find_time_hours; }
/** * {@inheritdoc} */ public static function toInternal($date) { $date = str_pad(str_pad($date, 16, '0', STR_PAD_LEFT), 32, '0', STR_PAD_RIGHT); $time = ['seconds' => gmp_strval(gmp_init('0x' . substr($date, 0, 16), 16), 10), 'nano' => gmp_strval(gmp_init('0x' . substr($date, 16, 8), 16), 10), 'atto' => gmp_strval(gmp_init('0x' . substr($date, 24, 8), 16), 10)]; if (bccomp($time['seconds'], bcpow(2, 63)) >= 0) { $time = ['seconds' => bcsub(bcpow(2, 63), 1, 0), 'nano' => '999999999', 'atto' => '999999999']; } return $time; }
public static function decode($encode, $number = 0) { $len = strlen($encode); $arr = array_flip(str_split(self::$base62)); for ($i = 0; $i < $len; $i++) { $number = bcadd($number, bcmul($arr[$encode[$i]], bcpow(62, $len - $i - 1))); } return $number; }
public function decode($var) { $length = strlen($var); $result = 0; for ($i = 0; $i < $length; $i++) { $result = bcadd($result, bcmul(self::getDigit($var[$i]), bcpow(self::$base, $length - ($i + 1)))); } return $result; }
/** * @see http://stackoverflow.com/questions/1273484/large-hex-values-with-php-hexdec * * @param string $hex * @return string */ public static function bigHexToBigDec($hex) { $dec = '0'; $len = strlen($hex); for ($i = 1; $i <= $len; $i++) { $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); } return $dec; }
function q16($number, $power) { $numberString = bcpow($number, $power); $numberLength = strlen($numberString); $sum = 0; for ($i = 0; $i < $numberLength; ++$i) { $sum += (int) $numberString[$i]; } return $sum; }
public static function unhash($hash) { $len = strlen($hash); $ceil = bcpow(62, $len); $mmiprimes = array_values(self::$golden_primes); $mmi = $mmiprimes[$len]; $num = self::unbase62($hash); $dec = bcmod(bcmul($num, $mmi), $ceil); return $dec; }
public function hex2dec($number) { $decvalues = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 'A' => '10', 'B' => '11', 'C' => '12', 'D' => '13', 'E' => '14', 'F' => '15'); $decval = '0'; $number = strrev($number); for ($i = 0; $i < strlen($number); $i++) { $decval = bcadd(bcmul(bcpow('16', $i, 0), $decvalues[$number[$i]]), $decval); } return round($decval); }
/** * Convert a number from base 62 to base 10 * * @param string $str Number * @return int Number converted to base 10 */ public static function decode($str) { $char_numbers = array_flip(str_split(self::$CHARS)); $len = strlen($str); $val = 0; for ($i = 0; $i < $len; ++$i) { $val = bcadd($val, bcmul($char_numbers[$str[$i]], bcpow(self::$BASE, $len - $i - 1))); } return $val; }
function decodeIMEI($input) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_'; $tot = '0'; for ($i = 0; $i < strlen($_GET['code']); $i++) { $n = strpos($chars, $_GET['code'][$i]); $tot = bcadd($tot, bcmul($n, bcpow(strlen($chars), strlen($_GET['code']) - $i - 1))); } return $tot; }
public function powerDigitSum($n) { $pow = bcpow(2, $n); $powString = strval($pow); $ret = 0; for ($i = 0; $i < strlen($powString); $i++) { $ret += intval($powString[$i]); } return $ret; }
/** * Suid::decode() * * Decodes a Short Universally Unique Identifier into an Integer * * @param string $suid * @return int $id */ public static function decode($suid) { $id = ''; $len = strlen($suid); for ($i = $len - 1; $i >= 0; $i--) { $value = strpos(self::$chars, $suid[$i]); $id = bcadd($id, bcmul($value, bcpow(34, $len - $i - 1))); } return $id; }
function q29($a, $b) { $values = []; for ($i = 2; $i <= $a; ++$i) { for ($j = 2; $j <= $b; ++$j) { $value = bcpow($i, $j); $values[$value] = true; } } return count($values); }
function random_word() { $random = $this->random; $bytes = $this->bytes; $count = $this->count; $bits = $random->urandom_bytes($bytes); $words = $this->words; $x = bchexdec(bin2hex($bits)); $wordnum = bcdiv(bcmul($x, $count), bcpow(2, 8 * $bytes), 0); return $words[$wordnum]; }
public function getResult() { if ($this->error == 0) { $this->esperance = ($this->n + 1) / 2; $this->variance = (bcpow($this->n, 2) - 1) / 12; $this->prob = 1 / $this->n; return array($this->n, $this->esperance, $this->variance, $this->prob); } else { return $this->error; } }
public function encodeId($id) { $base = strlen($this->alphabet); $out = ""; for ($t = floor(log10($id) / log10($base)); $t >= 0; $t--) { $a = floor($id / pow($base, $t)); $out = $out . substr($this->alphabet, $a, 1); $id = $id - $a * bcpow($base, $t); } return strrev($out); }