• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP gcd函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中gcd函数的典型用法代码示例。如果您正苦于以下问题:PHP gcd函数的具体用法?PHP gcd怎么用?PHP gcd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了gcd函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: Generate

 function Generate($level)
 {
     if ($level <= 3) {
         $num1 = pow(2, rand(2, 3));
         $num2 = pow(2, rand(4, 5));
     } elseif ($level <= 6) {
         $num1 = pow(2, rand(1, 3)) * pow(3, rand(0, 1));
         $num2 = pow(2, rand(1, 4)) * pow(3, rand(2, 3));
     } else {
         $num1 = pow(2, rand(1, 3)) * pow(3, rand(0, 3)) * pow(5, rand(0, 1));
         $num2 = pow(2, rand(1, 4)) * pow(3, rand(0, 3)) * pow(5, rand(2, 3));
     }
     $gcd = gcd($num1, $num2);
     $num3 = rand(1, 2) == 1 ? $gcd : $gcd / 2;
     // // Original exercise
     // $num1 = 48;
     // $num2 = 120;
     // $num3 = 12;
     // $gcd = gcd($num1, $num2);
     $correct = $num3 == $gcd ? 0 : 1;
     $options = ['Igaz', 'Hamis'];
     $solution = $options[$correct];
     $question = 'Adja meg az alábbi állítás logikai értékét (igaz vagy hamis)!<br />' . The($num1, TRUE) . ' $' . $num1 . '$ és ' . The($num2) . ' $' . $num2 . '$ legnagyobb közös osztója ' . The($num3) . ' $' . $num3 . '$.';
     $page[] = '<div class="alert alert-info"><strong>Közös osztó:</strong> az a szám, amivel mind a két szám osztható.</div>';
     $page[] = '<div class="alert alert-info"><strong>Legnagyobb közös osztó:</strong> a közös osztók közül a legnagyobb. Az $a$ és $b$ számok legnagyobb közös osztóját $(a;b)$-vel jelöljük.</div>';
     $page[] = 'A legnagyobb közös osztó kiszámításához először írjuk fel mindkét szám prímtényezős felbontását!';
     $hints[] = $page;
     $hints[][] = 'Az első szám prímtényezős felbontása: $' . $num1 . '=' . implode('\\cdot', $this->CanonicForm($num1)) . '$, ugyanis:' . $this->Factorization($num1);
     $hints[][] = 'A második szám prímtényezős felbontása: $' . $num2 . '=' . implode('\\cdot', $this->CanonicForm($num2)) . '$, ugyanis:' . $this->Factorization($num2);
     $page = [];
     $page[] = 'Most gyűjtsünk össze a közös prímtényezőket (ha mindkét számban előfordul, akkor a kisebb kitevőt nézzük): $$(' . $num1 . ';' . $num2 . ')=' . implode('\\cdot', $this->CanonicForm($gcd)) . '=' . $gcd . '$$';
     $page[] = 'Mivel a legnagyobb közös osztó ' . ($gcd == $num3 ? 'megegyezik' : 'nem egyezik meg') . ' ' . The($num3) . ' $' . $num3 . '$-' . With($num3) . ', ezért az állítás <span class="label label-success">' . strtolower($solution) . '</span>.';
     $hints[] = $page;
     return array('question' => $question, 'correct' => $correct, 'solution' => $solution, 'options' => $options, 'hints' => $hints);
 }
开发者ID:zsebtanar,项目名称:zsebtanar_v4,代码行数:35,代码来源:Osztas_teszt3.php


示例2: fract_label

function fract_label($v)
{
    # Add leading sign and make positive:
    if ($v < 0) {
        $result = '-';
        $v *= -1;
    } elseif ($v == 0) {
        $result = '0';
    } else {
        $result = '';
    }
    # Process whole number part.
    # Prepare for space between number and fraction.
    if ($v >= 1) {
        $whole = (int) $v;
        $v -= $whole;
        $result .= $whole;
        $space_between = ' ';
    } else {
        $space_between = '';
    }
    # Process fractional part:
    if ($v > 0) {
        $p64 = (int) round($v * 64);
        $factor = gcd($p64, 64);
        $numerator = $p64 / $factor;
        $denominator = 64 / $factor;
        $result .= $space_between . $numerator . '/' . $denominator;
    }
    return $result;
}
开发者ID:myfarms,项目名称:PHPlot,代码行数:31,代码来源:fractionticks.php


示例3: gcd

function gcd($a, $b)
{
    if ($b == 0) {
        return $a;
    }
    return gcd($b, $a % $b);
}
开发者ID:Nisaiy,项目名称:DeveloperClub,代码行数:7,代码来源:lcm.php


示例4: gcd

function gcd($a, $b)
{
    if (!b) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}
开发者ID:mjk0621,项目名称:code_algorithm,代码行数:8,代码来源:find_gcd.php


示例5: gcd

function gcd($a, $b)
{
    if ($a == 0) {
        return $b;
    } else {
        return gcd($b, $a % $b);
    }
}
开发者ID:henkerik,项目名称:typing,代码行数:8,代码来源:12.php


示例6: lcd

function lcd($a, $b)
{
    if ($a < $b) {
        $c = $a;
        $a = $b;
        $b = $c;
    }
    return $a * $b / gcd($a, $b);
}
开发者ID:juno,项目名称:code-snippet,代码行数:9,代码来源:lcd.php


示例7: ritmo

function ritmo($durações)
{
    $tempos = 0;
    $unidade = max($durações);
    foreach ($durações as $duração) {
        $tempos += $unidade / $duração;
    }
    $gcd = gcd($tempos, $unidade);
    $tempos /= $gcd;
    $unidade /= $gcd;
    return "{$tempos}/{$unidade}";
}
开发者ID:victorarias,项目名称:dojo-centro,代码行数:12,代码来源:ritmo.php


示例8: Generate

    function Generate($level)
    {
        $m = pow(-1, rand(1, 2)) * rand(1, 2);
        $b = rand(-5, 5);
        $A[0] = pow(-1, rand(1, 2)) * rand(1, 10);
        // Ax != 0
        $A[1] = $A[0] * $m + $b;
        $B[0] = -$A[0];
        $B[1] = $B[0] * $m + $b;
        // // Original exercise
        // $A = [-3,-1];
        // $B = [3,7];
        // $m = ($A[1]-$B[1])/($A[0]-$B[0]);
        // $b = $A[1] - $A[0]*$m;
        // print_r('m='.$m.', b='.$b.'<br />');
        // print_r('A('.$A[0].';'.$A[1].'), B('.$B[0].';'.$B[1].')	<br />');
        $mfrac['nom'] = ($B[1] - $A[1]) / gcd($B[1] - $A[1], $B[0] - $A[0]);
        $mfrac['denum'] = ($B[0] - $A[0]) / gcd($B[1] - $A[1], $B[0] - $A[0]);
        $question = 'Írja fel a hozzárendelési utasítását annak a lineáris függvénynek, mely $' . ($A[0] < 0 ? '(' . $A[0] . ')' : $A[0]) . '$-' . To($A[0]) . ' $' . ($A[1] < 0 ? '(' . $A[1] . ')' : $A[1]) . '$-' . Dativ($A[1]) . ' és $' . ($B[0] < 0 ? '(' . $B[0] . ')' : $B[0]) . '$-' . To($B[0]) . ' $' . ($B[1] < 0 ? '(' . $B[1] . ')' : $B[1]) . '$-' . Dativ($B[1]) . ' rendel! (A hozzárendelési utasítást $x\\mapsto mx+b$ alakban adja meg!)';
        $page[] = 'A hozzárendelés egy $y=mx+b$ alakú lineáris függvény lesz.';
        $page[] = 'A függvény $' . ($A[0] < 0 ? '(' . $A[0] . ')' : $A[0]) . '$-' . To($A[0]) . ' $' . ($A[1] < 0 ? '(' . $A[1] . ')' : $A[1]) . '$-' . Dativ($A[1]) . ' rendel, azaz:$$' . $A[1] . '=' . $A[0] . '\\cdot m+b$$';
        $page[] = 'Továbbá azt is tudjuk, hogy $' . ($B[0] < 0 ? '(' . $B[0] . ')' : $B[0]) . '$-' . To($B[0]) . ' $' . ($B[1] < 0 ? '(' . $B[1] . ')' : $B[1]) . '$-' . Dativ($B[1]) . ' rendel, azaz:$$' . $B[1] . '=' . $B[0] . '\\cdot m+b$$';
        $hints[] = $page;
        $page = [];
        $page[] = '$$\\begin{eqnarray}
			I.\\quad& ' . $A[1] . '&=&' . $A[0] . '\\cdot m+b\\\\
			II.\\quad& ' . $B[1] . '&=&' . $B[0] . '\\cdot m+b
		\\end{eqnarray}$$
			Vonjuk ki az első egyenletből a másodikat! Ekkor a $b$-s tagok kiesnek:$$\\begin{eqnarray}
			' . $A[1] . (-$B[1] < 0 ? '' : '+') . strval(-$B[1]) . '&=&(' . $A[0] . (-$B[0] < 0 ? '' : '+') . strval(-$B[0]) . ')\\cdot m+b-b\\\\
			' . strval($A[1] - $B[1]) . '&=&' . strval($A[0] - $B[0]) . '\\cdot m\\\\
			\\frac{' . strval($A[1] - $B[1]) . '}{' . strval($A[0] - $B[0]) . '}&=&m\\\\
		\\end{eqnarray}$$';
        $page[] = 'Tehát az $m$ értéke <span class="label label-success">$' . (round($m) == $m ? $m : '\\frac{' . $mfrac['nom'] . '}{' . $mfrac['denum'] . '}') . '$</span>.';
        $hints[] = $page;
        $page = [];
        $page[] = '$$\\begin{eqnarray}
			I.\\quad& ' . $A[1] . '&=&' . $A[0] . '\\cdot m+b\\\\
			II.\\quad& ' . $B[1] . '&=&' . $B[0] . '\\cdot m+b
		\\end{eqnarray}$$
		Most adjuk össze a két egyenletet! Ekkor az $m$-es tagok esnek ki:$$\\begin{eqnarray}
			' . $A[1] . ($B[1] < 0 ? '' : '+') . $B[1] . '&=&(' . $A[0] . ($B[0] < 0 ? '' : '+') . $B[0] . ')\\cdot m+b+b\\\\
			' . strval($A[1] + $B[1]) . '&=&2\\cdot b\\\\
			\\frac{' . strval($A[1] + $B[1]) . '}{2}&=&b\\\\
		\\end{eqnarray}$$';
        $page[] = 'Tehát a $b$ értéke <span class="label label-success">$' . $b . '$</span>.';
        $hints[] = $page;
        $correct = [round1($m), $b];
        $solution = '$m=' . round2($m) . '\\quad b=' . $b . '$';
        return array('question' => $question, 'correct' => $correct, 'type' => 'array', 'solution' => $solution, 'hints' => $hints, 'labels' => ['$m$', '$b$']);
    }
开发者ID:zsebtanar,项目名称:zsebtanar_v4,代码行数:51,代码来源:Egyenes_egyenlet.php


示例9: gcd

function gcd($m, $n)
{
    if ($m < $n) {
        return -1;
    }
    if ($n == 0) {
        return $m;
    }
    $m = $m % $n;
    if ($m == 0) {
        return $n;
    }
    return gcd($n, $m);
}
开发者ID:juno,项目名称:code-snippet,代码行数:14,代码来源:gcd.php


示例10: gcd

function gcd($x, $y)
{
    if ($x >= $y) {
        $a = $x;
        $b = $y;
    } else {
        $a = $y;
        $b = $x;
    }
    if ($b <= 0) {
        if ($b >= 0) {
            return $a + 0;
        }
    }
    return gcd($b + 0, $a - $b);
}
开发者ID:badlamer,项目名称:hhvm,代码行数:16,代码来源:gcd.php


示例11: wrr

/**
 * Weighted round-robin
 *
 * Adapted from LVS Weighted round-robin implementation
 * (@see http://kb.linuxvirtualserver.org/wiki/Weighted_Round-Robin_Scheduling)
 *
 * @param  array    $S  weights
 * @param  integer &$i  current position
 * @param  integer &$cw current weight
 * @return integer
 */
function wrr($S, &$i = -1, &$cw = 0)
{
    if (!($n = count($S))) {
        return null;
    }
    while (true) {
        $i = ($i + 1) % $n;
        if ($i == 0) {
            $cw = $cw - gcd($S);
            if ($cw <= 0) {
                $cw = max($S);
                if ($cw == 0) {
                    return null;
                }
            }
        }
        if ($S[$i] >= $cw) {
            return $i;
        }
    }
}
开发者ID:bdelespierre,项目名称:bloom-filter.php,代码行数:32,代码来源:lib.php


示例12: cryptorsakeys

function cryptorsakeys($p, $q)
{
    $n = $p * $q;
    $phi = ($p - 1) * ($q - 1);
    $e = 3;
    while (gcd($e, $phi) != 1 && $e < $phi) {
        $e++;
    }
    if ($e >= $phi) {
        echo 'e bigger than phi - fail';
        return;
    }
    list($d, $j, $k) = extended_gcd($e, $phi);
    if ($d < 0) {
        $d += $phi;
    }
    return array($n, $e, $d);
}
开发者ID:ericvanboven,项目名称:IMathAS,代码行数:18,代码来源:crypto.php


示例13: smd

function smd($num, $den)
{
    $g = gcd($num, $den);
    return $den / $g;
}
开发者ID:axomar,项目名称:ITS,代码行数:5,代码来源:solEval.php


示例14: fractionreduce

function fractionreduce()
{
    $args = func_get_args();
    $retarr = false;
    if (count($args) == 1) {
        if (is_array($args[0])) {
            $f = $args[0];
        } else {
            $f = fractionparse($args[0]);
        }
    } else {
        if (count($args) == 2 && is_array($args[0])) {
            $f = $args[0];
            $retarr = $args[1];
        } else {
            if (count($args) == 2) {
                $f = array($args[0], $args[1]);
            } else {
                if (count($args) == 3) {
                    $f = array($args[0], $args[1]);
                    $retarr = $args[2];
                }
            }
        }
    }
    $g = gcd($f[0], $f[1]);
    $f[0] /= $g;
    $f[1] /= $g;
    if ($f[1] < 0) {
        $f[0] *= -1;
        $f[1] *= -1;
    }
    if ($retarr) {
        return $f;
    }
    if ($f[1] == 1) {
        return $f[0];
    } else {
        return $f[0] . '/' . $f[1];
    }
}
开发者ID:ericvanboven,项目名称:IMathAS,代码行数:41,代码来源:fractions.php


示例15: fraction_divide

function fraction_divide($level)
{
    if ($level <= 3) {
        $num1 = rand(1, 2);
        $num2 = rand(1, 2);
        $denom1 = rand(1, 3);
        $denom2 = rand(1, 3);
    } elseif ($level <= 6) {
        $num1 = rand(3, 5);
        $num2 = rand(3, 5);
        $denom1 = rand(5, 10);
        $denom2 = rand(5, 10);
    } else {
        $num1 = rand(5, 10);
        $num2 = rand(5, 10);
        $denom1 = rand(10, 20);
        $denom2 = rand(10, 20);
    }
    $frac1 = $num1 / $denom1;
    $frac2 = $num2 / $denom2;
    $num = $num1 * $denom2;
    $denom = $denom1 * $num2;
    $gcd = gcd($num, $denom);
    if ($gcd) {
        $num /= $gcd;
        $denom /= $gcd;
    }
    $question = 'Mennyi lesz az alábbi művelet eredménye? $$\\frac{' . $num1 . '}{' . $denom1 . '}:\\frac{' . $num2 . '}{' . $denom2 . '}$$';
    $solution = '$\\frac{' . $num . '}{' . $denom . '}$';
    $correct = array($num, $denom);
    $type = 'fraction';
    return array('question' => $question, 'correct' => $correct, 'solution' => $solution, 'type' => $type);
}
开发者ID:zsebtanar,项目名称:zsebtanar_v4,代码行数:33,代码来源:functions_helper.php


示例16: simplexpivot

function simplexpivot($sm, $pivotpoint)
{
    if (!is_array($pivotpoint)) {
        $pivotpoint = explode(',', $pivotpoint);
    }
    $Pivotrow = $pivotpoint[0];
    $Pivotcol = $pivotpoint[1];
    $PivotValue = $sm[$Pivotrow][$Pivotcol];
    // change pivot point to a one
    for ($j = 0; $j < count($sm[$Pivotrow]); $j++) {
        $top = $sm[$Pivotrow][$j][0] * $PivotValue[1];
        $bot = $sm[$Pivotrow][$j][1] * $PivotValue[0];
        if ($bot < 0) {
            $top *= -1;
            $bot *= -1;
            // must be positive
        }
        $gcf = gcd($top, $bot);
        $top /= $gcf;
        $bot /= $gcf;
        $sm[$Pivotrow][$j] = array($top, $bot);
        // divide by $PivotValue
    }
    // now zero out all other values in that row
    for ($r = 0; $r < count($sm); $r++) {
        if ($r != $Pivotrow) {
            $PivotValue = array(-$sm[$r][$Pivotcol][0], $sm[$r][$Pivotcol][1]);
            for ($c = 0; $c < count($sm[$r]); $c++) {
                // multiplication
                $top = $PivotValue[0] * $sm[$Pivotrow][$c][0];
                $bot = $PivotValue[1] * $sm[$Pivotrow][$c][1];
                // addition
                $top = $top * $sm[$r][$c][1] + $sm[$r][$c][0] * $bot;
                $bot = $bot * $sm[$r][$c][1];
                if ($bot < 0) {
                    $top *= -1;
                    $bot *= -1;
                    // must be positive
                }
                $gcf = gcd($top, $bot);
                $top /= $gcf;
                $bot /= $gcf;
                $sm[$r][$c] = array($top, $bot);
            }
        }
    }
    return $sm;
}
开发者ID:ericvanboven,项目名称:IMathAS,代码行数:48,代码来源:simplex.php


示例17: gcd

<?php

require "example.php";
# Call our gcd() function
$x = "42 aaa";
$y = 105;
$g = gcd($x, $y);
print "The gcd of {$x} and {$y} is {$g}\n";
# Manipulate the Foo global variable
# Output its current value
print "Foo = " . Foo_get() . "\n";
# Change its value
Foo_set(3.1415926);
# See if the change took effect ( this isn't a good example for php, see
#				  manual for why. )
print "Foo = " . Foo_get() . "\n";
print_Foo();
开发者ID:msornay,项目名称:swig,代码行数:17,代码来源:runme.php


示例18: order_mod

 public static function order_mod($x, $m)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         if ($m <= 1) {
             return 0;
         }
         if (gcd($x, m) == 1) {
             $z = $x;
             $result = 1;
             while ($z != 1) {
                 $z = gmp_strval(gmp_Utils::gmp_mod2(gmp_mul($z, $x), $m));
                 $result = gmp_add($result, 1);
             }
             return gmp_strval($result);
         }
     } elseif (extension_loaded('bcmath') && USE_EXT == 'BCMATH') {
         if ($m <= 1) {
             return 0;
         }
         if (gcd($x, m) == 1) {
             $z = $x;
             $result = 1;
             while ($z != 1) {
                 $z = bcmod(bcmul($z, $x), $m);
                 $result = bcadd($result, 1);
             }
             return $result;
         }
     } else {
         throw new ErrorException("Please install BCMATH or GMP");
     }
 }
开发者ID:zebbra2014,项目名称:bitcoin-webskin-1,代码行数:32,代码来源:PHPCoinAddress.php


示例19: lowestTerm

function lowestTerm($e)
{
    $part = explode('/', $e);
    $gcd = gcd($part[0], $part[1]);
    return $gcd;
}
开发者ID:neilpeter08,项目名称:thesis101,代码行数:6,代码来源:analyze.blade.php


示例20: simplify

function simplify($var, $num)
{
    $result = 0;
    $var = str_replace('x', '', $var);
    if (fmod($num, $var) == 0) {
        $result = $num / $var;
    } else {
        $gcd = gcd($num, $var);
        $num /= $gcd;
        $var /= $gcd;
        $result = "{$num}/{$var} or " . round($num / $var, 2);
    }
    return $result;
}
开发者ID:neilpeter08,项目名称:thesis101,代码行数:14,代码来源:analyze-user-input.blade.php



注:本文中的gcd函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP gcms_fetch_object函数代码示例发布时间:2022-05-15
下一篇:
PHP gc_mem_caches函数代码示例发布时间:2022-05-15
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap