Skip to content
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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
65 changes: 56 additions & 9 deletions src/alg.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,71 @@
// Copyright 2025 UNN-CS
#include <cstdint>
#include "alg.h"

#include <cstdint>
#include <vector>

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<bool> 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;
}
5 changes: 2 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
70 changes: 69 additions & 1 deletion test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <cstdint>
#include "alg.h"


TEST(st1, sumPrime1) {
uint64_t res = sumPrime(2000000);
uint64_t expected = 142913828922;
Expand All @@ -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);
}
Loading