public function kirim_chat() { $this->load->view("fungsiRSA"); /* -- keterangan Masing Masing Fungsi yang dipake dari Library gmp -- gmp_div_qr = Bagi; gmp_add = Tambah; gmp_mul = Kali; gmp_sub = Kurang; gmp_gcd = Menghitung Nilai phi; gmp_strval = Convert Nomer ke String; */ // Inisialisasi P = 113 & Q = 157 (Masing Masing adalah Bilangan Prima) <--- Lebih Besar Lebih Bagus // Menghitung N = P*Q $n = gmp_mul(113, 157); $valn = gmp_strval($n); // Menghitung Nilai M =(p-1)*(q-1) $m = gmp_mul(gmp_sub(113, 1), gmp_sub(157, 1)); // Mencari E (Kunci Public --> (e,n)) // Inisialisasi E = 5 // Membuktikan E = FPB (Faktor Persekutuan Terbesar) dari E dan M = 1 for ($e = 5; $e < 1000; $e++) { // Mencoba dengan Perulangan 1000 Kali $fpb = gmp_gcd($e, $m); if (gmp_strval($fpb) == '1') { // Jika Benar E adalah FPB dari E dan M = 1 <-- Hentikan Proses break; } } // Menghitung D (Kunci Private --> (d,n)) // D = (($m * $i) + 1) / e = $key[1] <-- Perulangan Do While $i = 1; do { $key = gmp_div_qr(gmp_add(gmp_mul($m, $i), 1), $e); $i++; if ($i == 1000) { // Dengan Perulangan 1000 Kali break; } } while (gmp_strval($key[1]) != '0'); // Hasil D = $key[0] $d = $key[0]; $vald = gmp_strval($d); $user = $this->input->post("user"); $pesan = $this->input->post("pesan"); $userid = $this->input->post("iduser"); $hasilenkripsi = enkripsi($pesan, $n, $e); $insert = "insert into chat (user,pesan,id_user) VALUES ('{$user}','{$hasilenkripsi}','{$userid}')"; $this->db->query($insert); redirect("home/ambil_pesan"); }
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); }
/** * Gets crop's size. * * @param \Drupal\image\Entity\ImageStyle $image_style * The image style. * * @return string * The ratio to the lowest common denominator. */ public function getSizeRatio(ImageStyle $image_style) { // Get the properties of this ImageStyle. $properties = $this->getImageStyleSizes($image_style); if (isset($properties) && (!empty($properties['width']) || !empty($properties['height']))) { $gcd_object = gmp_gcd($properties['width'], $properties['height']); $gcd = gmp_strval($gcd_object); if (!empty($gcd) && $gcd != '1') { return round($properties['width'] / $gcd) . ':' . round($properties['height'] / $gcd); } elseif (!empty($gcd)) { return $gcd . ':' . $gcd; } else { return NULL; } } }
/** * @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; }
/** * Greatest cummon divisor */ function gcd($a, $b) { if (function_exists('gmp_gcd')) { $gcd = gmp_strval(gmp_gcd($a, $b)); $this->addDebug("gcd-version", "gmp_gcd:" . $gcd); return $gcd; } else { $gcd = $this->my_gcd($a, $b); $this->addDebug("gcd-version", "my_gcd:" . $gcd); return $gcd; } }
public function addPoints(array $pt1, array $pt2) { $p = $this->p; if (gmp_cmp($pt1['x'], $pt2['x']) == 0 && gmp_cmp($pt1['y'], $pt2['y']) == 0) { return $this->doublePoint($pt1); } $gcd = gmp_strval(gmp_gcd(gmp_sub($pt1['x'], $pt2['x']), $p)); if ($gcd != '1') { throw new \Exception('This library doesn\'t yet supports point at infinity. See https://github.com/BitcoinPHP/BitcoinECDSA.php/issues/9'); } // SLOPE = (pt1Y - pt2Y)/( pt1X - pt2X ) // Equals (pt1Y - pt2Y) * ( pt1X - pt2X )^-1 $slope = gmp_mod(gmp_mul(gmp_sub($pt1['y'], $pt2['y']), gmp_invert(gmp_sub($pt1['x'], $pt2['x']), $p)), $p); // nPtX = slope^2 - ptX1 - ptX2 $nPt = array(); $nPt['x'] = gmp_mod(gmp_sub(gmp_sub(gmp_pow($slope, 2), $pt1['x']), $pt2['x']), $p); // nPtX = slope * (ptX1 - nPtX) - ptY1 $nPt['y'] = gmp_mod(gmp_sub(gmp_mul($slope, gmp_sub($pt1['x'], $nPt['x'])), $pt1['y']), $p); return $nPt; }
$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 $gcd = gmp_gcd("12", "21"); echo gmp_strval($gcd) . "\n"; // gmp_gcdext $a = gmp_init(12); $b = gmp_init(21); $g = gmp_gcd($a, $b); $r = gmp_gcdext($a, $b); $check_gcd = gmp_strval($g) == gmp_strval($r['g']); $eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t'])); $check_res = gmp_strval($g) == gmp_strval($eq_res); if ($check_gcd && $check_res) { $fmt = "Solution: %d*%d + %d*%d = %d\n"; 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";
public static function gcd(Gmp $a, Gmp $b) { return new Gmp(gmp_gcd($a->getNumber(), $b->getNumber())); }
public function decript() { $this->load->view("fungsiRSA"); /* -- keterangan Masing Masing Fungsi yang dipake dari Library gmp -- gmp_div_qr = Bagi; gmp_add = Tambah; gmp_mul = Kali; gmp_sub = Kurang; gmp_gcd = Menghitung Nilai phi; gmp_strval = Convert Nomer ke String; */ // Inisialisasi P = 113 & Q = 157 (Masing Masing adalah Bilangan Prima) <--- Lebih Besar Lebih Bagus // Menghitung N = P*Q $n = gmp_mul(113, 157); $valn = gmp_strval($n); // Menghitung Nilai M =(p-1)*(q-1) $m = gmp_mul(gmp_sub(113, 1), gmp_sub(157, 1)); // Mencari E (Kunci Public --> (e,n)) // Inisialisasi E = 5 // Membuktikan E = FPB (Faktor Persekutuan Terbesar) dari E dan M = 1 for ($e = 5; $e < 1000; $e++) { // Mencoba dengan Perulangan 1000 Kali $fpb = gmp_gcd($e, $m); if (gmp_strval($fpb) == '1') { // Jika Benar E adalah FPB dari E dan M = 1 <-- Hentikan Proses break; } } // Menghitung D (Kunci Private --> (d,n)) // D = (($m * $i) + 1) / e = $key[1] <-- Perulangan Do While $i = 1; do { $key = gmp_div_qr(gmp_add(gmp_mul($m, $i), 1), $e); $i++; if ($i == 1000) { // Dengan Perulangan 1000 Kali break; } } while (gmp_strval($key[1]) != '0'); // Hasil D = $key[0] $d = $key[0]; $vald = gmp_strval($d); $user = $this->input->post("user"); $pesan = $this->input->post("pesan"); if ($pesan != "") { $hasildekripsi = deskripsi($pesan, $d, $n); ?> <li class="left clearfix"> <div class="chat-body clearfix"> <p class="bg-warning pesan"> <?php echo $hasildekripsi[1]; ?> </p> </div> </li> <?php } else { echo "<li class=\"right clearfix\">\r\n \r\n <div class=\"chat-body clearfix\">\r\n <p class=\"bg-warning pesan\">\r\n Belum ada data\r\n </p>\r\n </div>\r\n </li>"; } }
/** * Return Least Common Multiple of two numbers * @param int $a * @param int $b * @return int */ private function lcm($a, $b) { return gmp_abs(gmp_div_q(gmp_mul($a, $b), gmp_gcd($a, $b))); }
/** * * Reduce this number to it's lowest form */ protected function reduce() { $gcd = gmp_gcd($this->value['num']->gmp(), $this->value['den']->gmp()); if (gmp_cmp($gcd, 1) > 0) { $this->value['num']->set(gmp_div_q($this->value['num']->gmp(), $gcd)); $this->value['den']->set(gmp_div_q($this->value['den']->gmp(), $gcd)); } }
function question6() { $rand = rand(1, 100); $rand1 = rand(1, 100); $enonce = "PGCD(" . $rand . "," . $rand1 . ") ?"; $bonne = gmp_gcd($rand1, $rand); $mauvaise_list = array(0 => $bonne - rand(1, 5), 1 => $bonne + rand(1, 5), 2 => $bonne - rand(6, 10), 3 => $bonne - rand(6, 10)); shuffle($mauvaise_list); $bonne_rep = rand(0, 3); $retour = array(); for ($i = 0; $i < 4; $i++) { if ($i == $bonne_rep) { $retour[$i] = $bonne; } else { $retour[$i] = $mauvaise_list[$i]; } } return array("enonce" => $enonce, "bonne_rep" => $bonne_rep, "reponses" => $retour); }
gmp_gcd = Menghitung Nilai phi; gmp_strval = Convert Nomer ke String; */ // Inisialisasi P = 113 & Q = 157 (Masing Masing adalah Bilangan Prima) <--- Lebih Besar Lebih Bagus // Menghitung N = P*Q $n = gmp_mul(113, 157); echo "gmp mull :" . $n; $valn = gmp_strval($n); // Menghitung Nilai M =(p-1)*(q-1) $m = gmp_mul(gmp_sub(113, 1), gmp_sub(157, 1)); // Mencari E (Kunci Public --> (e,n)) // Inisialisasi E = 5 // Membuktikan E = FPB (Faktor Persekutuan Terbesar) dari E dan M = 1 for ($e = 5; $e < 1000; $e++) { // Mencoba dengan Perulangan 1000 Kali $fpb = gmp_gcd($e, $m); if (gmp_strval($fpb) == '1') { // Jika Benar E adalah FPB dari E dan M = 1 <-- Hentikan Proses break; } } // Menghitung D (Kunci Private --> (d,n)) // D = (($m * $i) + 1) / e = $key[1] <-- Perulangan Do While $i = 1; do { $key = gmp_div_qr(gmp_add(gmp_mul($m, $i), 1), $e); $i++; if ($i == 1000) { // Dengan Perulangan 1000 Kali break; }
/** * use PHP GMP function to get the largest common factor */ public function getLargestCommonFactor() { $gcd = gmp_gcd($this->num1, $this->num2); $f = gmp_strval($gcd); $this->largestFactor = (int) $f; }
protected function _getItemsData() { if ($this->usePriceRanges()) { $data = array(); if ($this->getInterval()) { return $data; } $priceRanges = $this->_getPriceRanges(); $priceRanges = explode(';', $priceRanges); foreach ($priceRanges as $priceRange) { $range = explode('-', $priceRange); $min = (int) $range[0]; $max = (int) $range[1]; if (0 === $min) { // from 0 to x $counts = $this->getRangeItemCounts($max); $count = 0; if (array_key_exists(1, $counts)) { $count = $counts[1]; } } elseif (0 === $max) { // from x to infinite $counts = $this->getRangeItemCounts($min); if (array_key_exists(1, $counts)) { unset($counts[1]); } $count = array_sum($counts); } else { // from x to y $range = array($min, $max); $min = min($range); $max = max($range); if (extension_loaded('gmp') && function_exists('gmp_gcd')) { $gcd = gmp_intval(gmp_gcd($min, $max)); } else { $gcd = gcd($min, $max); } $counts = $this->getRangeItemCounts($gcd); $count = 0; for ($i = (int) ($min / $gcd) + 1; $i * $gcd <= $max; $i++) { if (array_key_exists($i, $counts)) { $count += $counts[$i]; } } } if (0 < $count) { $range = explode('-', $priceRange); $data[] = array('label' => $this->_renderRangeLabel($range[0], $range[1]), 'value' => $priceRange, 'count' => $count); } } return $data; } return parent::_getItemsData(); }
<?php var_dump(gmp_strval(gmp_gcd(234, 12387))); var_dump(gmp_strval(gmp_gcd(0, 12387))); var_dump(gmp_strval(gmp_gcd(224, 0))); var_dump(gmp_strval(gmp_gcd(-1, 0))); var_dump(gmp_strval(gmp_gcd(-1, 0))); var_dump(gmp_strval(gmp_gcd("12371238123", "32618723123"))); var_dump(gmp_strval(gmp_gcd("7623456735", "12372341234"))); $n = gmp_init("8127346234"); var_dump(gmp_strval(gmp_gcd($n, "12372341234"))); $n = gmp_init("8127346234"); var_dump(gmp_strval(gmp_gcd("7623456735", $n))); $n = gmp_init("8127346234"); var_dump(gmp_strval(gmp_gcd($n, $n))); $n = gmp_init("8127346234"); var_dump(gmp_strval(gmp_gcd($n, 0))); var_dump(gmp_gcd($n, $n, 1)); var_dump(gmp_gcd($n, array(), 1)); var_dump(gmp_gcd(array(), $n, 1)); echo "Done\n";
/** * {@inheritdoc} */ public function gcd($a, $b) { return gmp_strval(gmp_gcd($a, $b)); }
/** * {@inheritdoc} */ public function gcd($left, $right) { return gmp_strval(gmp_gcd($left, $right)); }
/** * Finds greatest common divider (GCD) of $num1 and $num2 * * @param gmp resource $num1 * @param gmp resource $num2 * @return gmp resource * @access public */ function gcd($num1, $num2) { return gmp_gcd($num1, $num2); }
/** * This method returns the greatest common divisor. * * @access public * @static * @param IInteger\Type $x the left operand * @param IInteger\Type $y the right operand * @return IInteger\Type the result */ public static function gcd(IInteger\Type $x, IInteger\Type $y) : IInteger\Type { return IInteger\Type::box(gmp_strval(gmp_gcd($x->unbox(), $y->unbox()))); }
/** * Formats the value according to the index * * @param string Cell value * @param int Format index * * @return string Formatted cell value */ private function FormatValue($Value, $Index) { if (!is_numeric($Value)) { return $Value; } if (!empty($this->Styles[$Index])) { $Index = $this->Styles[$Index]; } else { return $Value; } $Format = array(); if (isset($this->ParsedFormatCache[$Index])) { $Format = $this->ParsedFormatCache[$Index]; } if (!$Format) { $Format = array('Code' => false, 'Type' => false, 'Scale' => 1, 'Thousands' => false, 'Currency' => false); if (isset(self::$BuiltinFormats[$Index])) { $Format['Code'] = self::$BuiltinFormats[$Index]; } elseif (isset($this->Formats[$Index])) { $Format['Code'] = $this->Formats[$Index]; } // Format code found, now parsing the format if ($Format['Code']) { $Sections = explode(';', $Format['Code']); $Format['Code'] = $Sections[0]; switch (count($Sections)) { case 2: if ($Value < 0) { $Format['Code'] = $Sections[1]; } break; case 3: case 4: if ($Value < 0) { $Format['Code'] = $Sections[1]; } elseif ($Value == 0) { $Format['Code'] = $Sections[2]; } break; } } // Stripping colors $Format['Code'] = trim(preg_replace('{^\\[[[:alpha:]]+\\]}i', '', $Format['Code'])); // Percentages if (substr($Format['Code'], -1) == '%') { $Format['Type'] = 'Percentage'; } elseif (preg_match('{^(\\[\\$[[:alpha:]]*-[0-9A-F]*\\])*[hmsdy]}i', $Format['Code'])) { $Format['Type'] = 'DateTime'; $Format['Code'] = trim(preg_replace('{^(\\[\\$[[:alpha:]]*-[0-9A-F]*\\])}i', '', $Format['Code'])); $Format['Code'] = strtolower($Format['Code']); $Format['Code'] = strtr($Format['Code'], self::$DateReplacements['All']); if (strpos($Format['Code'], 'A') === false) { $Format['Code'] = strtr($Format['Code'], self::$DateReplacements['24H']); } else { $Format['Code'] = strtr($Format['Code'], self::$DateReplacements['12H']); } } elseif ($Format['Code'] == '[$EUR ]#,##0.00_-') { $Format['Type'] = 'Euro'; } else { // Removing skipped characters $Format['Code'] = preg_replace('{_.}', '', $Format['Code']); // Removing unnecessary escaping $Format['Code'] = preg_replace("{\\\\}", '', $Format['Code']); // Removing string quotes $Format['Code'] = str_replace(array('"', '*'), '', $Format['Code']); // Removing thousands separator if (strpos($Format['Code'], '0,0') !== false || strpos($Format['Code'], '#,#') !== false) { $Format['Thousands'] = true; } $Format['Code'] = str_replace(array('0,0', '#,#'), array('00', '##'), $Format['Code']); // Scaling (Commas indicate the power) $Scale = 1; $Matches = array(); if (preg_match('{(0|#)(,+)}', $Format['Code'], $Matches)) { $Scale = pow(1000, strlen($Matches[2])); // Removing the commas $Format['Code'] = preg_replace(array('{0,+}', '{#,+}'), array('0', '#'), $Format['Code']); } $Format['Scale'] = $Scale; if (preg_match('{#?.*\\?\\/\\?}', $Format['Code'])) { $Format['Type'] = 'Fraction'; } else { $Format['Code'] = str_replace('#', '', $Format['Code']); $Matches = array(); if (preg_match('{(0+)(\\.?)(0*)}', preg_replace('{\\[[^\\]]+\\]}', '', $Format['Code']), $Matches)) { $Integer = $Matches[1]; $DecimalPoint = $Matches[2]; $Decimals = $Matches[3]; $Format['MinWidth'] = strlen($Integer) + strlen($DecimalPoint) + strlen($Decimals); $Format['Decimals'] = $Decimals; $Format['Precision'] = strlen($Format['Decimals']); $Format['Pattern'] = '%0' . $Format['MinWidth'] . '.' . $Format['Precision'] . 'f'; } } $Matches = array(); if (preg_match('{\\[\\$(.*)\\]}u', $Format['Code'], $Matches)) { $CurrFormat = $Matches[0]; $CurrCode = $Matches[1]; $CurrCode = explode('-', $CurrCode); if ($CurrCode) { $CurrCode = $CurrCode[0]; } if (!$CurrCode) { $CurrCode = self::$CurrencyCode; } $Format['Currency'] = $CurrCode; } $Format['Code'] = trim($Format['Code']); } $this->ParsedFormatCache[$Index] = $Format; } // Applying format to value if ($Format) { // Percentages if ($Format['Type'] == 'Percentage') { if ($Format['Code'] === '0%') { $Value = round(100 * $Value, 0) . '%'; } else { $Value = sprintf('%.2f%%', round(100 * $Value, 2)); } } elseif ($Format['Type'] == 'DateTime') { $Days = (int) $Value; // Correcting for Feb 29, 1900 if ($Days > 60) { $Days--; } // At this point time is a fraction of a day $Time = $Value - (int) $Value; $Seconds = 0; if ($Time) { // Here time is converted to seconds // Some loss of precision will occur $Seconds = (int) ($Time * 86400); } $Value = clone self::$BaseDate; $Value->add(new DateInterval('P' . $Days . 'D' . ($Seconds ? 'T' . $Seconds . 'S' : ''))); if (!$this->Options['ReturnDateTimeObjects']) { $Value = $Value->format($Format['Code']); } else { // A DateTime object is returned } } elseif ($Format['Type'] == 'Euro') { $Value = 'EUR ' . sprintf('%1.2f', $Value); } else { // Fractional numbers if ($Format['Type'] == 'Fraction' && $Value != (int) $Value) { $Integer = floor(abs($Value)); $Decimal = fmod(abs($Value), 1); // Removing the integer part and decimal point $Decimal *= pow(10, strlen($Decimal) - 2); $DecimalDivisor = pow(10, strlen($Decimal)); if (self::$RuntimeInfo['GMPSupported']) { $GCD = gmp_strval(gmp_gcd($Decimal, $DecimalDivisor)); } else { $GCD = self::GCD($Decimal, $DecimalDivisor); } $AdjDecimal = $DecimalPart / $GCD; $AdjDecimalDivisor = $DecimalDivisor / $GCD; if (strpos($Format['Code'], '0') !== false || strpos($Format['Code'], '#') !== false || substr($Format['Code'], 0, 3) == '? ?') { // The integer part is shown separately apart from the fraction $Value = ($Value < 0 ? '-' : '') . $Integer ? $Integer . ' ' : '' . $AdjDecimal . '/' . $AdjDecimalDivisor; } else { // The fraction includes the integer part $AdjDecimal += $Integer * $AdjDecimalDivisor; $Value = ($Value < 0 ? '-' : '') . $AdjDecimal . '/' . $AdjDecimalDivisor; } } else { // Scaling $Value = $Value / $Format['Scale']; if (@$Format['MinWidth'] && @$Format['Decimals']) { if ($Format['Thousands']) { $Value = number_format($Value, $Format['Precision'], self::$DecimalSeparator, self::$ThousandSeparator); } else { $Value = sprintf($Format['Pattern'], $Value); } $Value = preg_replace('{(0+)(\\.?)(0*)}', $Value, $Format['Code']); } } // Currency/Accounting if ($Format['Currency']) { $Value = preg_replace('', $Format['Currency'], $Value); } } } return $Value; }
public function isCoprime($number1, $number2) { $gcf = gmp_gcd($number1, $number2); return $this->equal($gcf, 1); }