開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Xcode - Apple
- Windows 10 Pro (OS)
- Visual Studio (コンパイラー)
- Visual Studio Code (Text Editor)
- C++17 (プログラミング言語)
Modern C++チャレンジ ―C++17プログラミング力を鍛える100問 (Marius Bancila(著)、島 敏博(監修)、黒川 利明(翻訳)、オライリージャパン)の1章(数学の問題)、問題12(最長コラッツ数列)の解答を求めてみる。
コード
#include <iostream> int main() { size_t n = 1; size_t len = 1; for (size_t i = 2; i <= 1000000; i++) { size_t len_t = 1; size_t j = i; while (j != 1) { len_t++; if (j % 2 == 0) { j /= 2; } else { j = j * 3 + 1; } } if (len_t > len) { len = len_t; n = i; } } std::cout << "数: " << n << " 長さ: " << len << std::endl; }
入出力結果(cmd(コマンドプロンプト)、Terminal)
Active code page: 65001 C:\Users\...>cl /O2 sample12.cpp && sample12.exe Microsoft(R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. sample12.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:sample12.exe sample12.obj 数: 837799 長さ: 525 C:\Users\...>
0 コメント:
コメントを投稿