File: count_primes3.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 (64 lines) | stat: -rw-r--r-- 1,451 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
59
60
61
62
63
64
///
/// @file   count_primes3.cpp
/// @brief  Count the primes within [10^12, 10^12 + 10^9]
///         using random sized intervals.
///
/// 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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <random>

using namespace std;
using namespace primesieve;

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

int main()
{
  cout << "Sieving the primes within [10^12, 10^12 + 10^9] randomly" << endl;

  uint64_t count = 0;
  uint64_t maxDist = (uint64_t) 1e7;
  uint64_t lowerBound = (uint64_t) 1e12;
  uint64_t upperBound = lowerBound + (uint64_t) 1e9;
  uint64_t start = lowerBound - 1;
  uint64_t stop = start;

  random_device rd;
  mt19937 gen(rd());
  uniform_int_distribution<uint64_t> dist(0, maxDist);

  while (stop < upperBound)
  {
    start = stop + 1;
    stop = min(start + dist(gen), upperBound);
    set_sieve_size(1 << (dist(gen) % 13));
    count += count_primes(start, stop);

    cout << "\rRemaining chunk:             "
         << "\rRemaining chunk: "
         << upperBound - stop << flush;
  }

  cout << endl << "Prime count: " << count;
  check(count == 36190991);

  cout << endl;
  cout << "Test passed successfully!" << endl;

  return 0;
}