Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jan 8, 2024
1 parent ac63cd4 commit 896c702
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/nthPrime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ uint64_t PrimeSieve::nthPrime(int64_t n, uint64_t start)
return prime;
}

/// Used for n < 0
uint64_t PrimeSieve::negativeNthPrime(int64_t n, uint64_t start)
{
ASSERT(n < 0);
Expand Down Expand Up @@ -147,7 +148,15 @@ uint64_t PrimeSieve::negativeNthPrime(int64_t n, uint64_t start)

// Here we are very close to the nth prime < sqrt(nth_prime),
// we simply iterate over the primes until we find it.
if (countApprox < n)
if (countApprox >= n)
{
uint64_t dist = (n - countApprox) * avgPrimeGap(start);
uint64_t stop = checkedAdd(start, dist);
primesieve::iterator iter(start, stop);
for (int64_t i = countApprox; i >= n; i--)
prime = iter.next_prime();
}
else // if (countApprox < n)
{
start = checkedSub(start, 1);
uint64_t dist = (countApprox - n) * avgPrimeGap(start);
Expand All @@ -160,14 +169,6 @@ uint64_t PrimeSieve::negativeNthPrime(int64_t n, uint64_t start)
throw primesieve_error("nth_prime(n): n is too small, nth_prime(n) < 2!");
}
}
else // if (countApprox >= n)
{
uint64_t dist = (n - countApprox) * avgPrimeGap(start);
uint64_t stop = checkedAdd(start, dist);
primesieve::iterator iter(start, stop);
for (int64_t i = countApprox; i >= n; i--)
prime = iter.next_prime();
}

auto t2 = std::chrono::system_clock::now();
std::chrono::duration<double> seconds = t2 - t1;
Expand Down

0 comments on commit 896c702

Please sign in to comment.