File: pi_table.cpp

package info (click to toggle)
primecount 7.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,336 kB
  • sloc: cpp: 13,107; makefile: 89; sh: 86; ansic: 30
file content (102 lines) | stat: -rw-r--r-- 2,290 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
///
/// @file   pi_table.cpp
/// @brief  Test the PiTable class
/// @link   https://en.wikipedia.org/wiki/Prime-counting_function
///
/// Copyright (C) 2021 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <PiTable.hpp>
#include <primesieve.hpp>

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

using std::size_t;
using namespace primecount;

std::vector<int> pix =
{
    0, 0, 1, 2, 2, 3, 3, 4, 4, 4,
    4, 5, 5, 6, 6, 6, 6, 7, 7, 8,
    8, 8, 8, 9, 9, 9, 9, 9, 9, 10,
    10, 11, 11, 11, 11, 11, 11, 12, 12, 12,
    12, 13, 13, 14, 14, 14, 14, 15, 15, 15,
    15, 15, 15, 16, 16, 16, 16, 16, 16, 17,
    17, 18, 18, 18, 18, 18, 18, 19, 19, 19,
    19, 20, 20, 21, 21, 21, 21, 21, 21
};

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

int main()
{
  // Test PiTable::pi_cache(x)
  {
    primesieve::iterator it;
    int64_t prime = it.next_prime();
    int64_t count = 0;

    for (int64_t i = 0; i <= PiTable::max_cached(); i++)
    {
      if (i == prime)
      {
        count++;
        prime = it.next_prime();
      }
      std::cout << "pi_cache(" << i << ") = " << PiTable::pi_cache(i);
      check(PiTable::pi_cache(i) == count);
    }
  }

  // Test PiTable::pi(x)
  {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(1000000, 2000000);

    int threads = 1;
    PiTable pi(dist(gen), threads);

    for (size_t i = 0; i < pix.size(); i++)
    {
      std::cout << "pi(" << i << ") = " << pi[i];
      check(pi[i] == pix[i]);
    }

    primesieve::iterator it;
    uint64_t prime = it.next_prime();
    int count = 1;

    while (prime < pi.size())
    {
      std::cout << "pi(" << prime << ") = " << pi[prime];
      check(pi[prime] == count);
      prime = it.next_prime();
      count++;
    }

    for (int i = 0; i < 10000; i++)
    {
      int n = dist(gen) % pi.size();
      std::cout << "pi(" << n << ") = " << pi[n];
      check(pi[n] == (int64_t) primesieve::count_primes(0, n));
    }
  }

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

  return 0;
}