CompProgLibrary

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub RTnF/CompProgLibrary

:heavy_check_mark: cpp/number_theory/is_prime.hpp

Depends on

Required by

Verified with

Code

#pragma once
#include "template/small_template.hpp"

// Miller test for 64bit
// https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
bool is_prime(ull n) {
  if (n == 2) {
    return true;
  }
  if (!(n & 1) || n <= 1) {
    return false;
  }
  auto powmod64 = [](ull x, ull y, ull mod) -> ull {
    ull pm = 1;
    while (y) {
      if (y & 1) {
        pm = (__uint128_t)pm * x % mod;
      }
      x = (__uint128_t)x * x % mod;
      y >>= 1;
    }
    return pm;
  };
  int r = 0;
  ull d = n - 1;
  while (!(d & 1)) {
    d >>= 1;
    ++r;
  }
  for (const ull p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
    if (p > n - 2) {
      break;
    }
    ull x = powmod64(p, d, n);
    if (x == 1 || x == n - 1) {
      continue;
    }
    bool cf = true;
    for (int i = 0; i < r - 1; ++i) {
      x = powmod64(x, 2, n);
      if (x == n - 1) {
        cf = false;
        break;
      }
    }
    if (cf) {
      return false;
    }
  }
  return true;
}
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus.py", line 187, in bundle
    bundler.update(path)
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 401, in update
    self.update(self._resolve(pathlib.Path(included), included_from=path))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.0/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 260, in _resolve
    raise BundleErrorAt(path, -1, "no such header")
onlinejudge_verify.languages.cplusplus_bundle.BundleErrorAt: template/small_template.hpp: line -1: no such header
Back to top page