0
0

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 5 years have passed since last update.

p+q=r2乗のN以下の素数を求める

Last updated at Posted at 2019-07-16

#問題文
p+q=r2 を満たす N 以下の素数 (p,q,r) の組の個数を求めてください。

#ソース

<?php
//入力値
$nyu = 10;

//素数を取得
for($i=2; $i<$nyu; ++$i){
    $flag = 0;
    for($j=2; $j<$i; ++$j){
        if( $i%$j == 0){
            $flag =1;
            break;
        }
    }
    if($flag == 0){
        $retsu[] = $i;
    }
}
//2乗の値を取得
for($i=0; $i<count($retsu); $i++){
    $total[] = $retsu[$i] * $retsu[$i];
}
//計算パターンを2次元配列に格納
foreach ($retsu as $val1) {
    foreach ($retsu as $val2) {
         $result[] = array_merge((array)$val1, (array)$val2);
    }
}
//配列に格納した組み合わせを足し算して、合計と一致するものにカウント+1
for($i=0; $i<count($retsu); $i++){
    for($p=0; $p<count($result); $p++){
        $tasu_kekka = ($result[$p][0] + $result[$p][1]);
        if($tasu_kekka == $total[$i]){
            $kekka[] = 1;
        }
    }
}

echo count($kekka);
?>
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?