LoginSignup
0
0

More than 3 years have passed since last update.

【PHP】問題集への解答

Posted at

ID 10

200万までの素数の合計値を求める

<?php
$start = microtime(true);
play();
$end = microtime(true);
print "\n".'処理時間 = ' . ($end - $start) . '秒'."\n" ;

function play()
{
  printf("%8d",2);
  $sum=2;

    for($i=3;$i<=2000000;$i+=2)
    {
        $k=0;
        for($j=3;$j<=sqrt($i);$j+=2)
        {
            if($i%$j==0)
            {
                $k=1;
                break;
            }
        }

        if($k==0) {
        // printf("%8d",$i);
        $sum+=$i;
      }
    }

    print "\n\n\n$sum\n\n\n" ;

}

ID 16

2^{15}=32768の3+5+2+7+6+8=26
では2^{1000}はなにか

とりあえず2の1000乗を見てみると

print 2**1000;1.0715086071863E+301

指数表記される。
E+Nというのは10のN乗という意味
1.0715086071863E+301

1.0715086071863☓10^{301}

という意味。

<?php
$start = microtime(true);
play();
$end = microtime(true);
print "\n".'処理時間 = ' . ($end - $start) . '秒'."\n" ;

function play(){
  $num=2**500;
  var_dump($num);
  if(strpos((string)$num,'E') !== false){
  //'abcd'のなかに'bc'が含まれている場合
  preg_match('/(.*)E/',(string)$num,$match);
  $num=$match[1];
  $num=str_replace('.', '', $num);

}
var_dump($num);
  preg_match_all('/(\d)/',(string)$num,$match);
  print_r((array_sum($match[0])));

}

53だと思ったが違うみたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0