I made the changes u suggested but, the program is still returning all odd numbers as prime.
Code:
/* miller-rabin */
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
bool is_prime(int p, int k,int a,int f)
{
int i=1,j,x,u;
bool prime=true;
while((prime) && i<=k)
{
u = 1 + rand() % (p-1);
x = fmod(pow(u,a),p);
if(x==1 || x==-1)
{
j = 1;
while((x!=-1 || x!=1) && j<=(f-1))
{
x = fmod(pow(x,2),p);
if(x==1)
{
prime = false;
//break;
}
j = j+1;
}
if(x==-1)
{
prime = false;
//break;
}
}
i++;
}
return (prime);
}
int main()
{
long unsigned int p,q,a=1,f=0,k;
bool compo=false;
cout<<"Enter the number: ";
cin>>p;
cout<<"Enter the security parameter: ";
cin>>k;
while(1)
{
a = 1;
while(p-1>=pow(2,f)*a)// to determine the values of f and a
{
if((p-1)==(pow(2,f)*a))
{
compo = is_prime(p,k,a,f);
}
a=a+2;
}
if(compo==true || (pow(2,f)) > p)
{
cout<<"\n"<<p<<" is composite\n";
break;
}
else if(compo==false)
{
cout<<"\n"<<p<<" is prime\n";
break;
}
f++;
}
return 0;
}