Conditional tests in primality by trial division

前端 未结 4 1668
清酒与你
清酒与你 2020-12-04 01:31

My question is about the conditional test in trial division. There seems to be some debate on what conditional test to employ. Let\'s look at the code for this from Roset

4条回答
  •  鱼传尺愫
    2020-12-04 02:20

    An answer to only a small portion of this post.

    Case 2 fix to deal with overflow.

    #include 
    
    int is_prime(unsigned n) {
      unsigned p;
      if (!(n & 1) || n < 2)
        return n == 2;
    
      #define UINT_MAX_SQRT (UINT_MAX >> (sizeof(unsigned)*CHAR_BIT/2))
      unsigned limit = n;
      if (n >= UINT_MAX_SQRT * UINT_MAX_SQRT)
        limit = UINT_MAX_SQRT * UINT_MAX_SQRT - 1;
    
      for (p = 3; p * p < limit; p += 2)
        if (!(n % p))
          return 0;
    
      if (n != limit)
        if (!(n % p))
          return 0;
      return 1;
    }
    

    The limit calculation fails if both sizeof(unsigned) and CHAR_BIT are odd - a rare situation.

提交回复
热议问题