0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

問題集への解答

Posted at

ID:2 フィボナッチ数列の生成

<?php
$t=[0,1];
$new=0;
$res=0;
for ($i=0; $new < 4000000; $i++) {
    $new=$t[0]+$t[1];
    if ($new%2==0) {
        $res+=$new;
    }
    //  print $new."\n";
    $t[0]=$t[1];
    $t[1]=$new;
}
  print $res."\n";

ID:3 素因数を求める

最初のコード

<?php
$start = microtime(true);


function findFactor($num)
{
    $count=0;
    for ($i=$num; $i >2 ; $i--) {
        if ($num%$i==0) {
            $count++;
        }
        if ($count==2) {
            return false;
        }
    }
    return true;
}

const NUM=600851475143;
  // const NUM=13195;

for ($i=NUM; $i >2 ; $i--) {
  // print $i."\n";
    if (NUM%$i==0 && findFactor($i)) {
        print $i."\n";
        break;
    }
}

$end = microtime(true);

print '処理時間 = ' . ($end - $start) . '秒'."\n" ;

何時まで立っても割らなかった。
Macもファンがガンガン回ってメモリ利用量が半端じゃなかった。

改良

<?php
$start = microtime(true);
$num=600851475143;
  
for ($i=2; $i <=$num ; $i++) {
  // print $i."\n";
    if ($num%$i==0) {
      $count=0;
        while($num%$i==0){
          $num/=$i;
          $count++;
        }
        print $i." e ".$count."\n";
    }
}

$end = microtime(true);

print '処理時間 = ' . ($end - $start) . '秒'."\n" ;

1から数えていく方式をやめた。

ID:4 回文を作成

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


function play()
{
    for ($i=999; $i >0 ; $i--) {
        for ($j=999; $j >0 ; $j--) {
            $num=$i*$j;
            // print $num."\n";
            if (checkPalindrome($num)) {
                $a[]=checkPalindrome($num);
            }
        }
    }
    print max($a);
}

function checkPalindrome($num)
{
    $num=(string)$num;

    if (strlen($num)%2==0 && strlen($num)>4) {
        if ($num[0]==$num[strlen($num)-1] && $num[1]==$num[strlen($num)-2] && $num[2]==$num[strlen($num)-3]) {
            return $num;
        }
    }
    return false;
}

checkPalindrome関数のif文が無理やり過ぎてもっといい方法がないものかと思った。

ID5 最小公倍数を求める

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


function play()
{
    print check(20);
}


function check($num)
{
    $res=[];
    $anser=1;
    for ($i=2; $i <=$num ; $i++) {
        // print "[".$i."]\n";
        $tmp=$i;
        for ($j=2; $j <=$tmp ; $j++) {
            $count=0;
            if ($tmp%$j==0) {
                while ($tmp%$j==0) {
                    $tmp/=$j;
                    $count++;
                }
                print $j." e ".$count."\n";
                $res[$j]=$res[$j]<$count? $count:$res[$j];
            }
        }
    }
    print_r($res);
    foreach ($res as $key => $value) {
        $anser*=$key**$value;
    }
    return $anser;
}

配列のところでエラーが出るが今回は無視

ID:6足してから2乗の合計-2乗の合計

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


function play()
{
  $sqSum=0;
  $sumSq=0;
    for ($i=1; $i <=100 ; $i++) {
      $sqSum+=$i**2;
      $sumSq+=$i;
    }
    
    print $sumSq**2-$sqSum;
}

ID7 10001番目の素数

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


function play()
{
$res=[];
for ($i=2; $i <1000000 ; $i++) {
  if(checkPrime($i)){
    print $i."\n";
    $res[]=$i;
  }
  if(count($res)>10001)break;
}
print_r($res);
}

function checkPrime($num){
  // print "[".$num."]\n";
  $count=0;
  for ($i=2; $i <$num ; $i++) {
    // print "$i\n";
    if ($num%$i==0) {
      return false;
   }
  }
  return true;
}

処理には1分かかった。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?