Skip to content

Commit 20aefee

Browse files
author
Nadim-Mahmud
committed
nuber theory
1 parent 1d61518 commit 20aefee

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// ***Eulers totients Sieve [n*n]
2+
/// Number of Co-prime upto a number
3+
4+
bool mr[MX];
5+
int pi[MX];
6+
7+
void phi(){
8+
int i,j;
9+
pi[1] = 1;
10+
for(i=2;i<MX;i++){
11+
if(!mr[i]){
12+
for(j=i;j<MX;j+=i){
13+
if(pi[j]==0) pi[j] = j;
14+
mr[j] = 1;
15+
pi[j] = pi[j]/i*(i-1);
16+
}
17+
}
18+
}
19+
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/// ***Miller-Rabin Primality test [120]
3+
4+
/** usigned long long is not working sometimes
5+
* left shift == mult by 2 right shift == divide bye 2
6+
* multiplying two numbers (a*b)%c avoiding overflow
7+
*/
8+
ll mulmod(ll a, ll b, ll mod){
9+
ll x = 0,y = a % mod;
10+
while (b > 0){
11+
if (b&1) x = (x + y) % mod;
12+
y = (y<<1) % mod;
13+
b >>= 1;
14+
}
15+
return x % mod;
16+
}
17+
18+
//Bigmod
19+
ll modulo(ll n, ll r, ll mod){
20+
ll x = 1;
21+
while (r > 0){
22+
if (r&1) x = mulmod(x,n,mod);
23+
n = mulmod(n,n,mod);
24+
r >>= 1;
25+
}
26+
return x % mod;
27+
}
28+
/// higher value of "it" ensure higher percision [recomendation 7]
29+
30+
bool MillerRabin(ll p,int it)
31+
{
32+
if (p < 2) return 0;
33+
if (p != 2 && p % 2==0) return 0;
34+
35+
ll i,a,tmp,mod,s=p-1;
36+
37+
while(s%2==0){
38+
s>>=1;
39+
}
40+
for(i=0;i<it;i++){
41+
a = rand()%(p-1)+1;
42+
tmp = s;
43+
mod = modulo(a, tmp, p);
44+
if(mod==1 || mod == p-1)continue;
45+
while (tmp!=p-1&&mod!=1&&mod!=p-1){
46+
mod = mulmod(mod, mod, p);
47+
tmp <<= 1;
48+
}
49+
if(mod!=p-1) return 0;
50+
}
51+
return 1;
52+
}
53+
54+
/** this body of miller rabin giving correct ans upto 10^9
55+
* with lower time complexity
56+
*/
57+
58+
for(i=1;i<=it;i++){
59+
a = rand() % (p - 1) + 1, tmp = s;
60+
mod = mod_pow(a, tmp, p);
61+
while(tmp != p - 1 && mod != 1 && mod != p - 1) {
62+
mod = mod_mul(mod, mod, p);
63+
tmp *= 2;
64+
}
65+
if (mod != p - 1 && tmp % 2 == 0) return false;
66+
}
67+
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/// *** Modular Inverse [ log(n) ]
3+
4+
/** [ large division = (upper%M)*modi(low)) ]
5+
* [ after modular division value should be moded ]
6+
* [ mainly it returns an iverse and multiplicable value ]
7+
* [ for loop == first loop throw all number(multiply) with simple mod -]
8+
* [- then modular inverse ]
9+
*/
10+
11+
#define M 1000000007
12+
#define pii pair<ll,ll>
13+
pii extnuc(ll a,ll b)
14+
{
15+
if(b==0)return pii(1,0);
16+
pii d=extnuc(b,a%b);
17+
return pii(d.second,d.first-d.second*(a/b));
18+
}
19+
20+
ll modi(ll n) /// n must be moded value
21+
{
22+
pii d=extnuc(n,M);
23+
return ((d.first%M)+M)%M;
24+
}
25+
26+
///Now factorial & inverse factorial with MOD
27+
void fact()
28+
{
29+
ll i;
30+
fr[0]=fi[0]=1;
31+
for(i=1;i<2000007;i++){
32+
fr[i]=(fr[i-1]*i)%M;
33+
fi[i]=(fi[i-1]*modi(i))%M;
34+
//P(fr[i])
35+
}
36+
}
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
/// ***Number of divisors [Quberoot(n)]
3+
4+
/** There can be only two prime after qube root
5+
* so we factorize upto quberoot by the trial divison then
6+
* handle ramainig <=2 prime
7+
* Miller Rabin needed
8+
*/
9+
10+
ll NOD(ll n){
11+
ll i,j,r,nod=1,cn;
12+
for(i=1;i<=in&&(pr[i]*pr[i]*pr[i])<=n;i++){
13+
cn = 1;
14+
while(n%pr[i]==0){
15+
n /= pr[i];
16+
cn++;
17+
}
18+
nod *= cn;
19+
}
20+
21+
r = sqrtl(n);
22+
while((r+1)*(r+1)<=n) r++;
23+
24+
if(MillerRabin(n,8)) nod *= 2;
25+
else if(r*r==n&&MillerRabin(r,8)) nod *= 3;
26+
else if(n!=1) nod *= 4;
27+
28+
return nod;
29+
}
30+

0 commit comments

Comments
 (0)