最大公約数と最小公倍数を求める

C++C++Beginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

数学において、最大公約数(GCD: Greatest Common Divisor)と最小公倍数(LCM: Least Common Multiple)は便利な概念です。2つの数の最大公約数は、それぞれの数の因数である最大の数であり、最大公約数は最大公約数とも呼ばれます。逆に、2つ以上の整数の最小公倍数は、それらすべての倍数である最小の正の整数です。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/BasicsGroup(["Basics"]) cpp(("C++")) -.-> cpp/ControlFlowGroup(["Control Flow"]) cpp(("C++")) -.-> cpp/IOandFileHandlingGroup(["I/O and File Handling"]) cpp(("C++")) -.-> cpp/SyntaxandStyleGroup(["Syntax and Style"]) cpp/BasicsGroup -.-> cpp/variables("Variables") cpp/ControlFlowGroup -.-> cpp/conditions("Conditions") cpp/ControlFlowGroup -.-> cpp/for_loop("For Loop") cpp/IOandFileHandlingGroup -.-> cpp/output("Output") cpp/IOandFileHandlingGroup -.-> cpp/user_input("User Input") cpp/IOandFileHandlingGroup -.-> cpp/files("Files") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("Code Formatting") subgraph Lab Skills cpp/variables -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/conditions -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/for_loop -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/output -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/user_input -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/files -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} cpp/code_formatting -.-> lab-96188{{"最大公約数と最小公倍数を求める"}} end

プロジェクトディレクトリを設定する

まず、mkdir コマンドを使用して新しいフォルダを作成し、cd コマンドを使用してそのディレクトリに C++ プロジェクトを設定します。たとえば:

mkdir ~/project
cd ~/project

新しい C++ ファイルを作成する

好きなテキストエディタを使って、main.cpp という名前の新しいファイルを作成します。たとえば:

touch main.cpp

C++ コードを記述する

次の C++ コードをコピーして main.cpp ファイルに貼り付けます。

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //変数宣言
    int n1, n2, i;

    //変数宣言と初期化
    int gcd = 1, lcm = 1;

    //コマンドライン(ユーザー)から入力を受け取る
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //2つの数の最大公約数と最小公倍数を計算する論理
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //iは両方の数を完全に割り切る最小の値であり、したがって最大公約数
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //2つの数の最大公約数と最小公倍数をコマンドラインに表示する
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using GCD and LCM calculator!\n\n\n";

    return 0;
}

コードをコンパイルして実行する

コードをコンパイルして実行するには、ターミナルで次のコマンドを使用します。

g++ main.cpp -o main && ./main

最終コード

ここに、すべてを一か所にまとめた完全なコードを再度示します。

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //変数宣言
    int n1, n2, i;

    //変数宣言と初期化
    int gcd = 1, lcm = 1;

    //コマンドライン(ユーザー)から入力を受け取る
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //2つの数の最大公約数と最小公倍数を計算する論理
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //iは両方の数を完全に割り切る最小の値であり、したがって最大公約数
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //2つの数の最大公約数と最小公倍数をコマンドラインに表示する
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using the GCD and LCM calculator!\n\n\n";

    return 0;
}

まとめ

おめでとうございます。2つの数の最大公約数(GCD)と最小公倍数(LCM)を計算するためのC++コードを学びました。これらのスキルを使えば、さまざまなアプリケーションに実装することができます。