File: count_primes1.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 (72 lines) | stat: -rw-r--r-- 1,665 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
///
/// @file   count_primes1.cpp
/// @brief  Count the primes up to 10^9.
///
/// Copyright (C) 2025 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <ParallelSieve.hpp>
#include <primesieve/Vector.hpp>

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

using namespace primesieve;

/// Correct pi(x) values to compare with test results
const Array<uint64_t, 9> pix =
{
  4,        // pi(10^1)
  25,       // pi(10^2)
  168,      // pi(10^3)
  1229,     // pi(10^4)
  9592,     // pi(10^5)
  78498,    // pi(10^6)
  664579,   // pi(10^7)
  5761455,  // pi(10^8)
  50847534  // pi(10^9)
};

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

int main()
{
  std::cout << std::left;
  ParallelSieve ps;
  uint64_t count = 0;
  uint64_t stop = 1;

  // pi(x) with x = 10^(i+1)
  for (size_t i = 0; i < pix.size(); i++)
  {
    uint64_t start = stop + 1;
    stop *= 10;
    count += ps.countPrimes(start, stop);
    std::cout << "pi(10^" << i + 1 << ") = " << std::setw(12) << count;
    check(count == pix[i]);
  }

  // Test PreSieve with preSievePrimes <= 163.
  // The thread interval must be sufficiently large
  // otherwise minimal pre-sieving is used.
  // Using a single thread increases thread interval.
  ps.setNumThreads(1);
  count = ps.countPrimes(0, (uint64_t) 1e9);
  std::cout << "pi(10^9) = " << std::setw(12) << count;
  check(count == 50847534);

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

  return 0;
}