Hi! I am having problems with this program, for every odd number it returns the number as prime.
here's my program
Code:/* Miller-Rabin Primality Test * By- manny721 * 15 oct 2011 * */ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<iostream> using namespace std; int main() { long unsigned int p,q,a=1,f=0,x,i=1,k,u,j; bool prime=true; cout<<"Enter the lower limit: "; cin>>p; //cout<<"Enter the upper limit: "; //cin>>q; cout<<"Enter the security parameter: "; cin>>k; while(1) { a = 1; if(p%2==0) { cout<<"\n"<<p<<" is composite\n"; break; } while(p-1>=pow(2,f)*a) { if((p-1)==(pow(2,f)*a)) { 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 = i+1; } } a = a+2; } if(prime==true) { cout<<"\n"<<p<<" is prime\n"; break; } else if(prime==false || (pow(2,f)) > p) { cout<<"\n"<<p<<" is composite\n"; break; } f = f+1; } return 0; } ]



LinkBack URL
About LinkBacks



