Skip to content

Instantly share code, notes, and snippets.

@lawrencefmm
Created May 16, 2019 18:19
Show Gist options
  • Save lawrencefmm/ca5605ff290f930bcbc90a075aeab78e to your computer and use it in GitHub Desktop.
Save lawrencefmm/ca5605ff290f930bcbc90a075aeab78e to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rd(random_device{}());
ll mul(ll a, ll b, ll c)
{
return __int128(a)*b%c;
}
ll expo(ll a, ll p, ll m)
{
ll t = 1;
while(p)
{
if(p & 1) t = mul(t, a , m);
a = mul(a, a, m);
p >>= 1;
}
return t;
}
bool checaprimalidade(int n, int iteracoes)
{
if(n < 4) return n == 2 || n == 3;
while(iteracoes--)
{
ll a = 2 + rd()%(n - 3);
if(expo(a, n - 1, n) != 1) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment