Skip to content

Задание 21.04.25 : 14.01 - 14.07 / 75 баллов / 30.04.25 23:59:59 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ include_directories(shared)


##### subdirectories #####
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11)
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11 week-17)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
Expand Down
4 changes: 4 additions & 0 deletions week-17/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(DIRS task-14-03)
foreach(DIR ${DIRS})
add_subdirectory(${DIR})
endforeach()
3 changes: 3 additions & 0 deletions week-17/task-14-03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(include)

add_subdirectory(test)
33 changes: 33 additions & 0 deletions week-17/task-14-03/include/pi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <future>
#include <random>
#include <thread>

double pi(const size_t iterations) {
const unsigned int available = std::thread::hardware_concurrency();
const size_t threads = (available == 0) ? 2 : available;
const size_t thread_iters = iterations / threads;
std::vector<std::future<size_t>> futures;
for (unsigned i = 0; i < threads; ++i) {
futures.emplace_back(std::async(std::launch::async, [thread_iters] {
size_t circle_points = 0;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution dist(0.0, 1.0);
for (size_t i = 0; i < thread_iters; ++i) {
const double x = dist(gen);
const double y = dist(gen);
if (x * x + y * y <= 1.0) {
circle_points++;
}
}
return circle_points;
}));
}
size_t circle_points = 0;
for (auto& f : futures) {
circle_points += f.get();
}
return static_cast<double>(4 * circle_points) / static_cast<double>(iterations);
}
1 change: 1 addition & 0 deletions week-17/task-14-03/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp_test(test-14-03.cpp)
4 changes: 4 additions & 0 deletions week-17/task-14-03/test/test-14-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "gtest/gtest.h"
#include "pi.hpp"

TEST(PiTest, PiTest) { EXPECT_NEAR(pi(1000000), 3.14, 0.01); }