2019年3月4日月曜日

開発環境

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

コード

#include <iostream>
#include "sample7_1.h"

void print_amicables(int const limit) {
  for (int number = 4; number < limit; ++number) {
    if (auto sum1 = sum_proper_divisors(number); sum1 < limit) {
      if (auto sum2 = sum_proper_divisors(sum1);
          sum2 == number && number < sum1) {
        std::cout << number << "," << sum1 << std::endl;        
      }
    }
  }
}

int main() {
  int limit = 0;
  std::cout << "Upper limit: ";
  std::cin >> limit;
  print_amicables(limit);
}

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

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

sample7_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:sample7_1.exe
sample7_1.obj
Upper limit: 1000000
220,284
1184,1210
2620,2924
5020,5564
6232,6368
10744,10856
12285,14595
17296,18416
63020,76084
66928,66992
67095,71145
69615,87633
79750,88730
100485,124155
122265,139815
122368,123152
141664,153176
142310,168730
171856,176336
176272,180848
185368,203432
196724,202444
280540,365084
308620,389924
319550,430402
356408,399592
437456,455344
469028,486178
503056,514736
522405,525915
600392,669688
609928,686072
624184,691256
635624,712216
643336,652664
667964,783556
726104,796696
802725,863835
879712,901424
898216,980984

C:\Users\...>

0 コメント:

コメントを投稿