File: nth_prime1.cpp

package info (click to toggle)
primesieve 7.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,080 kB
  • sloc: cpp: 7,270; ansic: 455; sh: 199; makefile: 86
file content (58 lines) | stat: -rw-r--r-- 1,168 bytes parent folder | download | duplicates (2)
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
58
///
/// @file   nth_prime1.cpp
/// @brief  Test nth_prime for |n| <= 9592
///
/// Copyright (C) 2017 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 std;
using namespace primesieve;

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

int main()
{
  uint64_t upperBound = (uint64_t) 1e5;
  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);
    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);
    cout << "nth_prime(" << n << ", " << upperBound << ") = " << res;
    check(res == primes[i]);
  }

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

  return 0;
}