sieve of eratosthenes
This commit is contained in:
parent
8f191a7e3f
commit
3c091551f5
4 changed files with 103 additions and 10 deletions
35
tests/test_sieve.cc
Normal file
35
tests/test_sieve.cc
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
|
||||
#include "../include/bmath/bmath.hh"
|
||||
|
||||
using namespace bmath;
|
||||
|
||||
bool naive_prime(size_t n) {
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
if (n == 2) {
|
||||
return true;
|
||||
}
|
||||
if (n % 2 == 0) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 3; i * i <= n; i += 2) {
|
||||
if (n % i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST(SieveTest, BasicConstruction) { Eratosthenes<100> sieve; }
|
||||
|
||||
TEST(SieveTest, ValidateAgainstNaive) {
|
||||
Eratosthenes<1000> sieve;
|
||||
|
||||
for (size_t i = 0; i <= 1000; ++i) {
|
||||
EXPECT_EQ(sieve[i], naive_prime(i))
|
||||
<< "Sieve disagrees with naive checker for " << i;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue