LoginSignup
2
1

More than 5 years have passed since last update.

合成数列の和アドベントカレンダー PHP 編

Last updated at Posted at 2018-12-02

合成数列の和アドベントカレンダー Advent Calendar 2018 の 3 日目です。

ルール

  • 入力として正の整数 N を与えたら 4 から始まる 合成数 の数列の 1 番目から N 番目からの合計を出力してください
  • N は最大で 100 とします

プログラム(PHP)

composite.php
<?php
// Extension load check
if (!extension_loaded('gmp'))
{
    die('PHP extension gmp is not loaded.');
}

$in = (int)$_SERVER['argv'][1];
$num = 4;
$composites = $sum = 0;
while ($composites < $in)
{
    if (!gmp_prob_prime($num))
    {
        $composites++;
        $sum += $num;
    }
    $num++;
}
print $sum;

※素数判定に GMPモジュール を使用しています。

出力例

$ php composite.php 2
10
$ php composite.php 4
27
$ php composite.php 10
112
$ php composite.php 100
7059

メモ

この問題は ずんだ問題 の番外編です

2
1
1

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