defceil_div(a:int,b:int)->intifb<=0raiseValueError("Denominator b must be positive.")ifa<0raiseValueError("Numerator a must be non-negative.")(a+b-1)//b
101112131415
defceil_div(a:int,b:int)->int:ifb<=0:raiseValueError("Denominator b must be positive.")ifa<0:raiseValueError("Numerator a must be non-negative.")return(a+b-1)//b
True when n is a prime: 2, 3, 5, 7, 11, ... prime(1) and prime(0) are
False, and so is every negative n. Trial division by 2, 3 and then
6k - 1 and 6k + 1 up to the square root, O(sqrt n).
Source code in pydsa/arith.mu
24252627282930313233343536373839
defprime(n:int)->bool"""True when n is a prime: 2, 3, 5, 7, 11, ... prime(1) and prime(0) are False, and so is every negative n. Trial division by 2, 3 and then 6k - 1 and 6k + 1 up to the square root, O(sqrt n)."""ifn<2returnFalseifn<4returnTrueifn%2==0orn%3==0returnFalseretd=5whiled*d<=nifn%d==0orn%(d+2)==0returnFalsed+=6True
3031323334353637383940414243444546
defprime(n:int)->bool:"""True when n is a prime: 2, 3, 5, 7, 11, ... prime(1) and prime(0) are False, and so is every negative n. Trial division by 2, 3 and then 6k - 1 and 6k + 1 up to the square root, O(sqrt n)."""ifn<2:returnFalseifn<4:returnTrueifn%2==0orn%3==0:returnFalsed=5whiled*d<=n:ifn%d==0orn%(d+2)==0:returnFalsed+=6Truereturnd
The primes from lo to hi, both ends included, in order; primes(n)
alone is the primes up to n, and primes(count=k) the first k primes.
primes(10) is [2, 3, 5, 7], primes(11, 29) is [11, 13, 17, 19, 23, 29]
and primes(count=4) is [2, 3, 5, 7].
A segmented sieve: the primes up to the square root of hi mark their
multiples in lo..hi alone, so a short range high up, primes(109,
109 + 100), costs the segment and not the billion below it. The
first k primes are sieved up to Rosser's bound k (ln k + ln ln k),
which the k-th prime is under from k = 6 on.
defprimes(lo:int?=none,hi:int?=none,count:int?=none)->[int]"""The primes from lo to hi, both ends included, in order; primes(n) alone is the primes up to n, and primes(count=k) the first k primes. primes(10) is [2, 3, 5, 7], primes(11, 29) is [11, 13, 17, 19, 23, 29] and primes(count=4) is [2, 3, 5, 7]. A segmented sieve: the primes up to the square root of hi mark their multiples in lo..hi alone, so a short range high up, primes(10**9, 10**9 + 100), costs the segment and not the billion below it. The first k primes are sieved up to Rosser's bound k (ln k + ln ln k), which the k-th prime is under from k = 6 on."""ifcountisnotnoneifloisnotnoneorhiisnotnoneraiseValueError("primes takes bounds or count, not both.")ifcount<=0return[]hi=15ifcount<6elseint(count*(log(count)+log(log(count))))+1found=primes(hi)whilelen(found)<countfound+=primes(hi+1,2*hi)hi*=2returnfound[:count]ifloisnoneraiseValueError("primes takes a bound or count.")ifhiisnonelo,hi=0,lolo=max(lo,2)ifhi<loreturn[]flags=[1]*(hi-lo+1)forpinprimes(isqrt(hi))forjinmax(p*p,ceil_div(lo,p)*p)..hibypflags[j-lo]=0[kforkinlo..hiifflags[k-lo]]
defprimes(lo:Optional[int]=None,hi:Optional[int]=None,count:Optional[int]=None)->list[int]:"""The primes from lo to hi, both ends included, in order; primes(n) alone is the primes up to n, and primes(count=k) the first k primes. primes(10) is [2, 3, 5, 7], primes(11, 29) is [11, 13, 17, 19, 23, 29] and primes(count=4) is [2, 3, 5, 7]. A segmented sieve: the primes up to the square root of hi mark their multiples in lo..hi alone, so a short range high up, primes(10**9, 10**9 + 100), costs the segment and not the billion below it. The first k primes are sieved up to Rosser's bound k (ln k + ln ln k), which the k-th prime is under from k = 6 on."""ifcountisnotNone:ifloisnotNoneorhiisnotNone:raiseValueError("primes takes bounds or count, not both.")ifcount<=0:return[]hi=15ifcount<6elseint(count*(log(count)+log(log(count))))+1found=primes(hi)whilelen(found)<count:found+=primes(hi+1,2*hi)hi*=2returnfound[:count]ifloisNone:raiseValueError("primes takes a bound or count.")ifhiisNone:lo,hi=0,lolo=max(lo,2)ifhi<lo:return[]flags=[1]*(hi-lo+1)forpinprimes(isqrt(hi)):forjinrange(max(p*p,ceil_div(lo,p)*p),hi+(1ifp>0else-1),p):flags[j-lo]=0return[kforkinrange(lo,hi+1)ifflags[k-lo]]