C++ における素数の識別

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

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

はじめに

この実験では、C++ で与えられた数が素数か合成数かどうかをチェックする方法を学びます。素数とは、1 より大きく、1 とそれ自身以外の任意の数で割り切れない数です。合成数とは、1 より大きく、1 とそれ自身以外の数で割り切れる数です。


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/BasicsGroup -.-> cpp/booleans("Booleans") cpp/ControlFlowGroup -.-> cpp/conditions("Conditions") cpp/ControlFlowGroup -.-> cpp/for_loop("For Loop") cpp/IOandFileHandlingGroup -.-> cpp/output("Output") cpp/IOandFileHandlingGroup -.-> cpp/user_input("User Input") subgraph Lab Skills cpp/booleans -.-> lab-96129{{"C++ における素数の識別"}} cpp/conditions -.-> lab-96129{{"C++ における素数の識別"}} cpp/for_loop -.-> lab-96129{{"C++ における素数の識別"}} cpp/output -.-> lab-96129{{"C++ における素数の識別"}} cpp/user_input -.-> lab-96129{{"C++ における素数の識別"}} end

isPrime() 関数を定義する

整数を受け取り、その整数が素数かどうかを表すブール値を返す isPrime() 関数を定義します。

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime(int n)
{
    if (n == 1)
        return false; // 1は素数ではない

    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

上記のコードでは、与えられた数が任意の数で割り切れるかどうかをチェックしています。そのような数がなければ、その数は素数です。数が1の場合、それは素数ではありません。

main() 関数を書く

ここでは、ユーザーから入力を受け取り、与えられた数が素数か合成数かを判定するために isPrime() 関数を使用する main() 関数を書きます。

int main()
{
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " ===== Most Efficient Program to determine if the entered number is Prime or not. ===== \n\n";

    cout << " ===== Note: 1 is neither Prime nor Composite. ===== \n\n";

    int n;
    bool prime = false;

    cout << " Enter a positive integer other than 1 :  ";
    cin >> n;

    prime = isPrime(n);

    if (prime)
    {
        cout << "\n\nThe entered number " << n << " is a Prime number.";
    }
    else
    {
        cout << "\n\nThe entered number " << n << " is Composite number.";
    }

    cout << "\n\n\n";

    return 0;
}

プログラムをコンパイルして実行する

次のように g++ コマンドを使用してコードをコンパイルします。

g++ main.cpp -o main

その後、./main コマンドを使用してプログラムを実行します。

./main

プログラムは、1以外の正の整数を入力するように促しますので、数値を入力してエンターキーを押します。

その後、プログラムは入力された数が素数か合成数かを出力します。

main.cpp の完全なコード

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime(int n)
{
    if (n == 1)
        return false; // 1は素数ではない

    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }

    return true;
}

int main()
{
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " ===== Most Efficient Program to determine if the entered number is Prime or not. ===== \n\n";

    cout << " ===== Note: 1 is neither Prime nor Composite. ===== \n\n";

    int n;
    bool prime = false;

    cout << " Enter a positive integer other than 1 :  ";
    cin >> n;

    prime = isPrime(n);

    if (prime)
    {
        cout << "\n\nThe entered number " << n << " is a Prime number.";
    }
    else
    {
        cout << "\n\nThe entered number " << n << " is Composite number.";
    }

    cout << "\n\n\n";

    return 0;
}

まとめ

この実験では、C++ で与えられた数が素数か合成数かをチェックする方法を学びました。与えられた数が1とそれ自身以外の数で割り切れるかどうかに基づいて、その数が素数であるかどうかを判定する関数を使用しました。その後、この関数を main() で使用して、ユーザーからの入力を受け取り、その数が素数か合成数かを出力しました。