dsbm49845 2010-03-02 19:35
浏览 20
已采纳

模数PHP问题

I have a problem, I am trying to calculate what the lowest prime is of a number but I do not understand the result that PHP is giving me.

If I have this number

 $number = 600851475143;

Then I modulus it:

$primes = array( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);

    foreach($primes as $key=>$value) {    
        if($number % $value == 0 ) {echo $value; break; }
    }

Why is it that $value = 3? If $value = 3, that means that 600851475143 / 3 should be an integer, but its not. So I do not understand why that if() evaluates to true?

  • 写回答

3条回答 默认 最新

  • du131642 2010-03-02 19:47
    关注

    Using PHP 5.2.8, it fails as described in the question. It seems like the modulo operator doesn't work on big integers. You should consider using bc_mod (modulus with arbitrary precision):

    <?php
    
    $number = 600851475143;
    
    $primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
    
        foreach($primes as $key=>$value)
        {    
            if($number % $value == 0 ) {echo $value."<br/>"; }
            if(bcmod($number, $value) == 0) {echo "bcmod ".$value."<br/>"; }
        }
    
    ?>
    

    The above code prints:

    3

    29

    bcmod 71

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么