Prime number "template"

Source: Internet
Author: User

Ordinary prime number judgment

int isprime (int N)      //Note #include<cmath>{    if (N <= 1)  return 0;    int i;    for (i = 2; I <= sqrt (n*1.0); ++i)        if (n%i = = 0)            return 0;    return 1;}

Sieve method for primes [1,n]

const int MAXN = 1000000;bool prime[maxn+100];   Prime[i] = = True means I is a prime number void IsPrime () {for    (int i = 2; I <= maxn; ++i)        prime[i] = true;    for (int i = 2; I <= MAXN, ++i)        if (Prime[i]) for            (int j = i+i; J <= Maxn; j+=i)                prime[j] = false;}

Two-time sieve method for primes [l,r]

BOOL prime[50010];    Deposit 50000 inside prime judgment result int primer[1000010];   The prime number of bool prime1[1000010] between the storage interval [l,r];    Determines whether the number in the interval [l,r] is a prime int isprime ()//The prime in the first sieve 50000 {int num = 0;    for (int i = 2; I <= 50000; i++) prime[i] = true;            for (int i = 2; I <= 50000; i++) {if (Prime[i]) {primer[num++] = i;        for (int j = i+i; J <= 50000; j+=i) prime[j] = false;     }} return num; Num is a number of elements in the range of 50000}int solve (__int64 A,__int64 b)/* On the basis of the first sieve prime, using the prime number within 50000, the number of pixels between the range "a", the remainder is the prime */{int num =    IsPrime ();    memset (prime1,true,sizeof (Prime1)); The Prime1 array is used to hold the primality of the range "A, B" to determine if (a = = 1)//Note here that 1 is not prime prime1[0] = 0; This indicates that 0+1 is not a prime number for (__int64 i = 0; i < num && primer[i] * Primer[i] <= b; i++) {__int64 begin =        A/primer[i] + (a%primer[i]! = 0);  The upper A/primer calculates how many times a is a prime primer[i] (a%primer[i]!=0) to start the sieve from a/primer[i] times of primer[i], or a/primer[i]+1 times sieve if (begin = = 1)//If the result is 1 time times the number of sieved prime, the sieve begin++ starts from twice times the prime number;    for (begin = begin*primer[i]; begin <= B; begin + = Primer[i]) prime1[begin-a] = false;    }//The Primer array is re-used here to hold the prime number between the interval "a, b", Num is the prime count of Memset (primer,0,sizeof (Primer));    num = 0;    for (__int64 i = A; I <= b; i++) if (prime1[i-a]==1) primer[num++] = i-a;     return num; Num is the number of characters in the interval [a, b]}

Test method for Miller prime number

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define Max_val (POW (    2.0,60)) #define LL __int64/*ll Mod_mul (ll x,ll y,ll mo)//calculate x * y% mo{LL t;    x%= mo;    for (t = 0; y; x = (x<<1)%mo,y>>=1) if (Y & 1) t = (t+x)%mo; return t;}    */ll Mod_mul (LL x,ll y,ll mo)//calculate x * y% mo{LL T,t,a,b,c,d,e,f,g,h,v,ans;    T = (LL) (sqrt (double (MO) +0.5));    t = T*t-mo;    A = x/t;    b = x% T;    c = y/t;    d = y% T;    e = a*c/t;    f = a*c% T;    v = ((a*d+b*c)%mo + e*t)% Mo;    g = v/t;    h = v% T;    Ans = (((f+g) *t%mo + b*d)% mo + h*t)%mo;    while (ans < 0) ans + = mo; return ans;}    ll Mod_exp (ll num,ll t,ll mo)//calculate num^t% mo{LL ret = 1, temp = num% mo;    for (; t; t >>=1,temp=mod_mul (Temp,temp,mo)) if (T & 1) ret = Mod_mul (RET,TEMP,MO); return ret;}    BOOL Miller_rabbin (LL N)//miller_rabbin primality test {if (n = = 2) return true; if (N < 2|| !     (n&1))    return false;    int t = 0;    LL a,x,y,u = n-1;        while ((u & 1) = = 0) {t++;    U >>= 1;        } for (int i = 0; i < i++) {a = rand ()% (n-1) +1;        x = Mod_exp (a,u,n);            for (int j = 0; J < T; j + +) {y = Mod_mul (x,x,n);            if (y = = 1 && x! = 1 && x! = n-1) return false;        x = y;    } if (x! = 1) return false; } return true;



Prime number "template"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.