I wrote the following code for finding prime numbers between two given integers n,m
Instead of the standard sieve method I tried using the Miller-Rabin test.
http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
But the following code is giving a wrong answer in prime1, please help.
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<numeric>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define inf 1000000007
#define mp make_pair
#define pb push_back
#define f(i,j) for(int i=0;i<j;i++)
#define fe(i,j) for(int i=0;i<=j;i++)
#define fr(i,j) for(int i=j;i>0;i--)
#define fre(i,j) for(int i=j;i>=0;i--)
//2 7 61
map<long long int,long long int> prm;
long long int t[3]={2,7,61};
long long int mod_pow(long long int a,long long int b,long long int n){
long long int res=1;
while(b){
if(b%2){
res=(res*a)%n;
}
a=(a*a)%n;
b/=2;
}
return res;
}
pair<long long int,long long int> thed(long long int n){
long long int cnt=0;
while(n%2==0){
n/=2;
cnt++;
}
return mp(n,cnt);
}
int isprime(long long int n){
if(n==1){
return 0;
}
else if(n==2){
return 1;
}
else if(n==3){
return 1;
}
else if(n==5){
return 1;
}
else if(n==7){
return 1;
}
else if(n==61){
return 1;
}
else{
long long int a,x,d,flag;
int s;
pair<long long int,long long int> ast=thed(n-1);
d=ast.first;
s=ast.second;
f(i,3){a=t[i];
x=mod_pow(a,d,n);
if(x==1 or x==n-1){
continue;
}
else{
flag=1;
f(i,s-1){
x=(x*x)%n;
if(x==1){
return 0;
}
if(x==n-1){
flag=0;
break;
}
}
if(flag){
return 0;
}
}
}
return 1;
}
}
int main(){
int t;
long long int n,m;
cin>>t;
while(t--){
cin>>m>>n;
for(long long int i=m;i<=n;i++){
if(isprime(i)){
cout<<i<<endl;
}
}
cout<<endl;
}
}