File: nth_prime1.cpp

package info (click to toggle)
primesieve 12.12%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,952 kB
  • sloc: cpp: 16,515; ansic: 723; sh: 531; makefile: 91
file content (57 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
///
/// @file   nth_prime1.cpp
/// @brief  Test nth_prime for |n| <= 9592
///
/// Copyright (C) 2022 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve.hpp>

#include <stdint.h>
#include <iostream>
#include <vector>
#include <cstdlib>

using namespace primesieve;

void check(bool OK)
{
  std::cout << "   " << (OK ? "OK" : "ERROR") << "\n";
  if (!OK)
    std::exit(1);
}

int main()
{
  uint64_t upperBound = (uint64_t) 1e5;
  std::vector<uint64_t> primes;
  generate_primes(upperBound, &primes);
  int64_t i = primes.size() - 1;
  int64_t n = 0;

  // nth_prime(n) forwards
  for (uint64_t prime : primes)
  {
    n++;
    uint64_t res = nth_prime(n);
    std::cout << "nth_prime(" << n << ") = " << res;
    check(res == prime);
  }

  // nth_prime(-n, start) backwards
  for (n = 0; i >= 0; i--)
  {
    n--;
    uint64_t res = nth_prime(n, upperBound);
    std::cout << "nth_prime(" << n << ", " << upperBound << ") = " << res;
    check(res == primes[i]);
  }

  std::cout << std::endl;
  std::cout << "All tests passed successfully!" << std::endl;

  return 0;
}