Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:23:00+00:00 2026-06-09T02:23:00+00:00

I am implementing Wikipedia’s Miller-Rabin algorithm but don’t seem to be getting even vaguely

  • 0

I am implementing Wikipedia’s Miller-Rabin algorithm but don’t seem to be getting even vaguely apt results. 7, 11, 19, 23 etc. are reported composite. Infact, when k>12, even 5 is shown composite. I have read the maths behind Miller-Rabin but don’t quite understand it very well and am relying on the algorithm blindly. Any cues on where I’m going wrong?

Here’s my code:

#include<stdio.h>
#include<math.h>

int modpow(int b, int e, int m) {
    long result = 1;

    while (e > 0) {
        if ((e & 1) == 1) {
            result = (result * b) % m;
        }
        b = (b * b) % m;
        e >>= 1;
    }

    return result;
}

int isPrime(long n,int k){
        int a,s,d,r,i,x,loop;
        if(n<2)return 0;
        if(n==2||n==3)return 1;
        if(n%2==0)return 0;

        d=n-1;
        s=0;
        while(d&1==0){
                d>>=1;
                s++;
        }


        for(i=0;i<k;i++){
                loop=0;
                a=(int)(rand()*(n-1))+1;
                x=modpow(a,d,n);
                if(x==1 || x==n-1){
                        continue;

                }
                for(r=1;r<=s;r++){
                        x=modpow(x,2,n);
                        if(x==1)return 0;
                        if(x==n-1){
                                loop=1;
                                break;
                        }
                }
                if(!loop)return 0;

        }
        return 1; 

}

int main(){
        int i,k;
        scanf("%d",&k);
        for(i=5;i<100;i+=2){
                printf("%d : %d\n",i,isPrime(i,k));
        }
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:23:02+00:00Added an answer on June 9, 2026 at 2:23 am

    If the base is not coprime to the candidate, the strong Fermat check always returns “not a probable prime”.

    With your mistake

    a=(int)(rand()*(n-1))+1;
    

    for a prime p, the base is not coprime to p (a multiple of p), if and only if the result of rand() has the form

    k*p + 1
    

    For small primes, that is practically guaranteed to happen even with few iterations.

    The base should lie between 2 ans n/2 (choosing bases larger than n/2 is not necessary since a is a witness for compositeness if and only if n - a is one), so you want something like

    a = rand() % (n/2 - 2) + 2;
    

    if you don’t mind the modulo bias in the random number generation that favours small remainders, or

    a = rand() /(RAND_MAX + 1.0) * (n/2 - 2) + 2;
    

    if you want to distribute the bias over the entire possible range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing the Verhoeff algorithm for a check digit scheme, but there seems to
I am implementing Hill cipher depending on the explanation Wikipedia But I want to
I'm implementing the SHA1 algorithm from the pseudocode on wikipedia. It says I should
I'm implementing the A* search algorithm from this pseudo on wikipedia's article: function A*(start,goal)
I am interested in implementing the Rabin-Karp algorithm to search for sub strings as
I'm implementing the shunting-yard algorithm . I'm having trouble detecting when there are missing
I work on implementing IPv6 support for several applications, but I wondered what are
This is the well know select algorithm. see http://en.wikipedia.org/wiki/Selection_algorithm . I need it to
So by now I'm getting the point that we should all be implementing our
I don't know if this is a dumb question but I have this two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.