File: alpha_lmo.cpp

package info (click to toggle)
primecount 8.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,648 kB
  • sloc: cpp: 21,887; ansic: 121; sh: 100; makefile: 89
file content (102 lines) | stat: -rw-r--r-- 2,464 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
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   alpha_lmo.cpp
/// @brief  Test the alpha tuning factor with the LMO algorithm.
///         y = alpha * x^(1/3)
///         By computing pi(x) using different alpha tuning
///         factors we can make sure that all array sizes
///         (and other bounds) are accurate.
///
/// Copyright (C) 2023 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <primecount-internal.hpp>
#include <imath.hpp>

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

using namespace primecount;

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

int main()
{
  int threads = get_num_threads();

  // Test small x
  {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int64_t> dist(100, 1000);

    for (int i = 0; i < 100; i++)
    {
      int64_t x = dist(gen);
      int64_t res1 = pi_cache(x);

      for (double alpha = 1; alpha <= iroot<6>(x); alpha++)
      {
        set_alpha(alpha);
        int64_t res2 = pi_lmo_parallel(x, threads);
        std::cout << "alpha = " << alpha << ", pi_lmo_parallel(" << x << ") = " << res2;
        check(res2 == res1);
      }
    }
  }

  // Test medium x
  {
    int64_t min = (int64_t) 1e3;
    int64_t max = (int64_t) 2e7;

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int64_t> dist(min, max);

    for (int i = 0; i < 50; i++)
    {
      int64_t x = dist(gen);
      int64_t res1 = pi_meissel(x, threads);

      for (double alpha = 1; alpha <= iroot<6>(x); alpha++)
      {
        set_alpha(alpha);
        int64_t res2 = pi_lmo_parallel(x, threads);
        std::cout << "alpha = " << alpha << ", pi_lmo_parallel(" << x << ") = " << res2;
        check(res2 == res1);
      }
    }
  }

  // Test large x
  {
    int64_t x = 9999999929ll;
    int64_t res1 = 455052509ll;
    std::vector<double> alphas = { 1, 1+1/3.0, 2, 10, (double) iroot<6>(x) };

    for (double alpha : alphas)
    {
      set_alpha(alpha);
      int64_t res2 = pi_lmo_parallel(x, threads);
      std::cout << "alpha = " << alpha << ", pi_lmo_parallel(" << x << ") = " << res2;
      check(res2 == res1);
    }
  }

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

  return 0;
}