You are on page 1of 2

The fast-exponentiation method: an implementation in C++:

long long int fast_exp(int base, int exp)

if(exp==1)

return base;

else

if(exp%2 == 0)

long long int base1 = pow(fast_exp(base, exp/2),2);

if(base1 >= 1000000007)

return base1%1000000007;

else

return base1;

else

long long int ans = (base* pow(fast_exp(base,(exp-1)/2),2));

if(ans >= 1000000007)

return ans%1000000007;

else

return ans;

}
}

ll fast_exp(int base, int exp) {

ll res=1;

while(exp>0) {

if(exp%2==1) res=(res*base)%MOD;

base=(base*base)%MOD;

exp/=2;

return res%MOD;

To Find Prime

If you do not find a factor less than x−−√, then x is prime for the following reason. Consider the
opposite, you find two factors larger than x−−√, say a and b. But then a⋅b>x−−√x−−√=x.
Therefore, if there is a factor larger than x−−√, there must also exist a factor smaller than x−
−√, otherwise their product would exceed the value of x.

You might also like