Implementation of Rabin Miller test in c++
<p>I've been trying to implement Rabin miller test in c++, and I have come up with the code below. But it is returning true value for lots of composite numbers eg:- 99999999999999989 etc, moreover it is also not working as fast as is expected from a probabilistic algorithm.
Can you point out the flaws in the code, Any advice on how to better the code would be appreciated</p>

<pre><code>   int modulo(int base,int exp,int mod)
{
long long  res=1,b=base;
while(exp&gt;0)
{
    if((exp &amp;1)==1)
        res=(res*b)%mod;
    exp=exp&gt;&gt;1;
    b=(b*b)%mod;
}
return res%mod;
}

 long long mulmod(long long a ,long long b,long long c)
{
long long res=0,y=a%c;
while(b&gt;0)
{
    if((b&amp;1)==1)
        res=(res+y)%c;
    y=(y&lt;&lt;1)%c;
    b=b&gt;&gt;1;
}
return res%c;
 }
 bool millers(long long p,int iteration)
 {
if(p&lt;2)
    return 0;
if(p!=2 &amp;&amp; p%2==0)
    return 0;
long long s=p-1;
while(s%2==0)
    s=s/2;

for(int i=0;i&lt;iteration;i++)
{
    long long a=rand()%(p-1)+1,temp=s;
    long long mod=modulo(a,temp,p);
    while(temp!=p-1 &amp;&amp; mod!=1 &amp;&amp; mod!=p-1){
        mod=mulmod(mod,mod,p);
        temp *= 2;
    }
    if(mod!=p-1 &amp;&amp; temp%2==0){
        return 0;
    }
}
return 1;
}
</code></pre>
