簡體   English   中英

帶有線程的素數

[英]Prime number with threading

我編寫代碼是為了從頭到尾打印用戶編寫的所有數字。 我想通過線程來做到這一點。 例如,開始是1,結束是100。我要求用戶輸入一個N數,即程序創建的線程數。 例如,如果他輸入10,程序將創建10個線程。 第一個線程將打印質數從1到10。第二個線程將打印質數從10到20。第三個線程將打印質數從10到20。

但是我有一個問題。 實際上,我的程序在文件中打印出許多不是素數的數字,並且比我在代碼中多次打印出的相同數字還要多。

這是我的代碼:

void writePrimesToFile(int begin, int end, ofstream&  file)
{
    for (int i = begin; i <= end; i++)
    {
        for (int j = begin; j < end / 2; j++)
        {
            if (i % j != 0)
            {
                file << i << endl;
            }

        }
    }
}

void callWritePrimesMultipleThreads(int begin, int end, string filePath, int N)
{
    ofstream myfile(filePath);
    clock_t startTimer, stopTimer;

    startTimer = clock();

    vector<thread> arr;

    for (int i = 0; i < N; i++)
    {
        int start = begin;
        int finish = N;
        arr.emplace_back(writePrimesToFile, start, finish, ref(myfile));
        start = finish;
        finish += N;
    }
    for (auto& thread : arr)
    {
        thread.join();
    }

    stopTimer = clock();
    cout << "The time that takes is: " << (double)(stopTimer - startTimer) / CLOCKS_PER_SEC << endl;
}

代碼中的主要內容:

    callWritePrimesMultipleThreads(1, 100, "primes2.txt", 10);

代碼中需要修復的很多事情,質數將從1開始,而不是0,也應該開始除以2而不是1或0(不能除以0)(得到0等於1的余數)不是質數,它總是以您要計算的數字結尾(10%20是沒有意義的)

#include <stdio.h>
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <functional>
#include <fstream>
#include <math.h>

using namespace std;
mutex mtx;

void writePrimesToFile(unsigned int begin, unsigned int end, ofstream& f)
{
    for (unsigned int i = begin; i <= end; i++)
    {
        for (unsigned int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            else if(j + 1 == i)
            {
                mtx.lock();
                f << i << endl;
                mtx.unlock();
            }
        }
    }
}

void callWritePrimesMultipleThreads(unsigned int begin, unsigned int end, string filePath, unsigned int N)
{
    ofstream myfile(filePath);
    clock_t startTimer, stopTimer;

    startTimer = clock();

    vector<thread> arr;
    unsigned int each = end/N;
    unsigned int start = begin;
    unsigned int finish = start + each - 1;
    for (unsigned int i = 0; i < N; i++)
    {
        arr.emplace_back(writePrimesToFile, start, finish, ref(myfile));
        start += each;
        finish += each;
    }
    for (auto& thread : arr)
    {
        thread.join();
    }

    stopTimer = clock();
    cout << "The time that takes is: " << (double)(stopTimer - startTimer) / CLOCKS_PER_SEC << endl;
}


int main()
{
    callWritePrimesMultipleThreads(1, 110, (string)"primes.txt", 10);
    return 0;
}

另外,在寫入文件時添加了互斥鎖。

看一下你的循環:

for (int i = begin; i <= end; i++)
{
    for (int j = begin; j < end / 2; j++)
    {
        if (i % j != 0)
        {
            file << i << endl;
        }

    }
}

每當您找到不被其整除的數字時,您就輸出i
有很多數字。
(例如9不能被2、4、5、6、7或8整除。但這不是質數。)

如果數字不能被任何數(> = 2)整除,則它是質數,如果不能被任何數整除,則不是素數。

beginend / 2之間尋找因子也是不夠的,您需要在2sqrt(end)之間尋找。

我的建議是在開始多線程和間隔切片之前,首先編寫一個有效的單線程素數測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM