-1

我想求两个数之间的公分之和,下面是我的代码,输出是 maghsom moshtarak is=24612 但目标只有 12;请帮我,

<?php
$m=0;
$j=6;
$h=12;

function SumCommon($j,$h)
{
 echo"maghsom moshtarak is=";  
    for($i=1;$i<=$h;$i++)
    {
        if(($j%$i==0)&&($h%$i==0))
        {

            Sum($i);

        }
    }
}

function Sum($i)
{
    $i+=$i;
    $m=$i;
    echo $m;  
}


SumCommon($j,$h);
?>
4

1 回答 1

0
<?php
$m=0;
$j=6;
$h=12;

function SumCommon($j,$h)
{
 echo "maghsom moshtarak is=";  
    for($i=1;$i<=$h;$i++)
    {
        if(($i%$j==0)&&($i%$h==0)) /* this is changed */
        {

            Sum($i);

        }
    }
}

function Sum($i)
{
    /* no need to use $i+=$i */
    $m=$i;
    echo $m;  
}

SumCommon($j,$h);
?>

** 精炼代码 **

<?php
$j=6;
$h=12;

function SumCommon($j,$h)
{
 echo "maghsom moshtarak is=";  
    for($i=1;$i<=$h;$i++)
    {
        if(($i%$j==0)&&($i%$h==0)) /* this is changed */
        {

            echo $i;

        }
    }
}

SumCommon($j,$h);
?>
于 2013-05-14T20:17:38.243 回答