diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5de0996 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +/build* +docs/build* +/xml +docs/xml +out +mpich +cmake-build-* +.idea/ +.vs/ +.vscode/ +scripts/variants*.csv +scripts/variants*.xlsx +*venv* +sln/ +CMakeSettings.json +.DS_Store +.cache +install +*.pyc diff --git a/src/alg.cpp b/src/alg.cpp index 93f44be..c4ab837 100644 --- a/src/alg.cpp +++ b/src/alg.cpp @@ -1,24 +1,71 @@ // Copyright 2025 UNN-CS -#include #include "alg.h" +#include +#include bool checkPrime(uint64_t value) { - // вставьте код функции + if (value < 2) { + return false; + } + if (value == 2 || value == 3) { + return true; + } + for (uint64_t i = 2; i * i <= value; i++) { + if ((value % i) == 0) { + return false; + } + } return true; } uint64_t nPrime(uint64_t n) { - // вставьте код функции - return 2; + if (n == 0) { + return 0; + } + + uint64_t position = 0; + uint64_t desired = 1; + + while (position < n) { + desired++; + if (checkPrime(desired)) + position++; + } + return desired; } uint64_t nextPrime(uint64_t value) { - // вставьте код функции - return 2; + if (value == UINT64_MAX) { + return 0; + } + + uint64_t next = value + 1; + while (!checkPrime(next)) next++; + return next; } uint64_t sumPrime(uint64_t hbound) { - // вставьте код функции - return 2; -} + if (hbound <= 2) { + return 0; + } + + std::vector is_prime(hbound, true); + is_prime[0] = false; + is_prime[1] = false; + + for (uint64_t i = 2; i * i < hbound; i++) { + if (is_prime[i]) { + for (uint64_t j = i * i; j < hbound; j += i) { + is_prime[j] = false; + } + } + } + uint64_t sum = 0; + for (uint64_t i = 0; i < hbound; i++) { + if (is_prime[i]) { + sum += i; + } + } + return sum; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e66c6d8..62ce77b 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,7 +3,7 @@ include(FetchContent) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.11.0 + GIT_TAG release-1.12.0 ) FetchContent_MakeAvailable(googletest) @@ -28,5 +28,4 @@ target_link_libraries(${PROJECT_NAME}.test #target_link_libraries(GTest::GTest INTERFACE gtest_main) #target_link_libraries(${PROJECT_NAME}.test ${PROJECT_NAME}) # target_compile_definitions(${PROJECT_NAME}.test -# PRIVATE TEST_DIR="${CMAKE_CURRENT_LIST_DIR}/test") - +# PRIVATE TEST_DIR="${CMAKE_CURRENT_LIST_DIR}/test") \ No newline at end of file diff --git a/test/tests.cpp b/test/tests.cpp index 3980306..d3e110a 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -4,7 +4,6 @@ #include #include "alg.h" - TEST(st1, sumPrime1) { uint64_t res = sumPrime(2000000); uint64_t expected = 142913828922; @@ -15,3 +14,72 @@ TEST(st1, sumPrime2) { uint64_t expected = 17; EXPECT_EQ(expected, res); } + +TEST(CheckPrimeTest, SmallPrimes) { + EXPECT_TRUE(checkPrime(2)); + EXPECT_TRUE(checkPrime(3)); + EXPECT_TRUE(checkPrime(5)); + EXPECT_TRUE(checkPrime(7)); +} + +TEST(CheckPrimeTest, SmallNonPrimes) { + EXPECT_FALSE(checkPrime(0)); + EXPECT_FALSE(checkPrime(1)); +} + +TEST(CheckPrimeTest, NonPrimes) { + EXPECT_FALSE(checkPrime(4)); + EXPECT_FALSE(checkPrime(9)); + EXPECT_FALSE(checkPrime(15)); + EXPECT_FALSE(checkPrime(25)); +} + +TEST(CheckPrimeTest, LargePrime) { + EXPECT_TRUE(checkPrime(982451653)); +} + +TEST(NPrimeTest, ZeroIndex) { + EXPECT_EQ(nPrime(0), 0); +} + +TEST(NPrimeTest, FirstPrimes) { + EXPECT_EQ(nPrime(1), 2); + EXPECT_EQ(nPrime(2), 3); + EXPECT_EQ(nPrime(3), 5); + EXPECT_EQ(nPrime(4), 7); + EXPECT_EQ(nPrime(5), 11); +} + +TEST(NPrimeTest, LargerPrimes) { + EXPECT_EQ(nPrime(10), 29); + EXPECT_EQ(nPrime(25), 97); +} + +TEST(NextPrimeTest, NextAfterPrimes) { + EXPECT_EQ(nextPrime(3), 5); + EXPECT_EQ(nextPrime(7), 11); + EXPECT_EQ(nextPrime(13), 17); +} + +TEST(NextPrimeTest, NextAfterSmall) { + EXPECT_EQ(nextPrime(1), 2); + EXPECT_EQ(nextPrime(2), 3); + EXPECT_EQ(nextPrime(4), 5); + EXPECT_EQ(nextPrime(10), 11); +} + +TEST(NextPrimeTest, MaxValue) { + EXPECT_EQ(nextPrime(UINT64_MAX), 0); +} + +TEST(SumPrimeTest, SumBelowSmallBounds) { + EXPECT_EQ(sumPrime(2), 0); + EXPECT_EQ(sumPrime(3), 2); + EXPECT_EQ(sumPrime(6), 10); +} + +TEST(SumPrimeTest, SumBelowLargerBounds) { + EXPECT_EQ(sumPrime(11), 17); + EXPECT_EQ(sumPrime(12), 28); + EXPECT_EQ(sumPrime(20), 77); +} \ No newline at end of file