2019年3月3日日曜日

開発環境

Modern C++チャレンジ ―C++17プログラミング力を鍛える100問 (Marius Bancila(著)、島 敏博(監修)、黒川 利明(翻訳)、オライリージャパン)の1章(数学の問題)、問題5(セクシー素数)の応用問題の解答を求めてみる。

コード

#include <iostream>
#include "prime.h"
int main()
{
  int limit = 0;

  std::cout << "Upper limit: ";
  std::cin >> limit;

  for (int c = 3; c <= 5; c++)
  {
    std::cout << c << "組のセクシー素数" << std::endl;
    for (int n = 2; n <= limit; n++)
    {
      bool are_sexy_primes = true;
      for (int m = n; m <= n + 6 * (c - 1); m += 6)
      {
        if (!is_prime(m))
        {
          are_sexy_primes = false;
          break;
        }
      }
      if (are_sexy_primes)
      {
        std::cout << "(";
        for (int m = n; m <= n + 6 * (c - 1); m += 6)
        {
          std::cout << m;
          if (m != n + 6 * (c - 1))
          {
            std::cout << ",";
          }
        }
        std::cout << ")" << std::endl;
      }
    }
  }
}

入出力結果(VS 2017 用 x64 Native Tools コマンド プロンプト、Terminal)

C:\Users\...>cl /O2 sample5_1.cpp && sample5_1.exe
Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

sample5_1.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ 例外処理を使っていますが、アンワインド セマンティクスは有効にはなりません。/EHsc を指定してください。
Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:sample5_1.exe
sample5_1.obj
Upper limit: 1000
3組のセクシー素数
(5,11,17)
(7,13,19)
(11,17,23)
(17,23,29)
(31,37,43)
(41,47,53)
(47,53,59)
(61,67,73)
(67,73,79)
(97,103,109)
(101,107,113)
(151,157,163)
(167,173,179)
(227,233,239)
(251,257,263)
(257,263,269)
(271,277,283)
(347,353,359)
(367,373,379)
(557,563,569)
(587,593,599)
(601,607,613)
(607,613,619)
(641,647,653)
(647,653,659)
(727,733,739)
(941,947,953)
(971,977,983)
4組のセクシー素数
(5,11,17,23)
(11,17,23,29)
(41,47,53,59)
(61,67,73,79)
(251,257,263,269)
(601,607,613,619)
(641,647,653,659)
5組のセクシー素数
(5,11,17,23,29)

C:\Users\...>

0 コメント:

コメントを投稿