#!/usr/local/bin/bc -l primes.bc primes_db_minipack.bc ### Primes_DB_Code.BC - prime counting / retrieval functions reliant on a DB ## Technical details: ## . Modulo 2310 [=primorial(11)] there are 480 possible primes ## . Each array entry in the pd_[] packed prime data array should represent ## . the primality of the 480 possibilities within the integers from ## . 2310*index to 2310*(index+1)-1 ## . The last 24 bits of each array entry should be the cumulative count of ## . . actual primes up to the end of that entry. ## . Primes 2 to 11 are handled seperately as they are not relatively prime ## . . to 2310 ## . This file and its minipack and bigpack siblings are designed to replace ## . . the older, unpacked, primes_db.bc. The prime[] array from the latter ## . . can be recreated using the fillprimearray() function herein. max_array_ = 4^8-1 # Create metadata arrays which allow the below to operate correctly define makemods2310_() { auto oib,c,i; oib=ibase;ibase=A if(!pd_2310_[239]){c=0;for(i=1;i<=1155;i++)if(int_gcd(i,2310)==1)pd_2310_[c++]=i} if(!pd_0112_[max_array_]){for(c=1;c<=32768;c+=c)for(i=c;i=(c=pd_[h]%cm)){ print "prime() knows only the first ",c+5," primes\n"; scale=os;return -1 } l=0;m=(l+h)/2;.=makemods2310_() while(l<=m){ k=pd_[m];hk=k/cm;c=k-hk*cm;k=hk if(c0)b=pd_[m-1]%cm if(b>n){ h=m-1 } else { # main routine ma = 1+max_array_ i=-1;while(k&&b<=n){ if(n-b<16){ hk=k/2;if(k-hk-hk).=b++;.=i++;k=hk } else { hk=k/ma;b+=pd_0112_[k-ma*hk];i+=16;k=hk # addition to i should be log2(max_array_+1) } } if(i<240){ m= m *2310+pd_2310_[ i] } else { m=(m+1)*2310-pd_2310_[479-i] } # end if i scale=os;ibase=oib;return m } # end if b } # end if c m=(l+h)/2 } # end while scale=os;ibase=oib;return -1 } # Return the number of primes up to (and including) p # . as long as p is less than or equal to the maximum db entry define primepi(p){ auto oib,os,ma,q,f,i,b,l,m,h,k; if(p<0)return -primepi(-p); p=prevprime_ifnotprime(p); if(p<2)return 0;if(p<3)return 1;if(p<5)return 2 if(p<7)return 3;if(pq){l=m+1}else{h=m-1} m=(l+h)/2 } ibase=oib;scale=os;return m } q=(1+pd_max_)*2310-1 if(p>q){ #q=prevprime_ifnotprime(q) print "primepi() only knows the count of primes up to ";q ibase=oib;scale=os;return -1 } .=makemods2310_() i=p/2310; q=p-i*2310 f=0;if(q>1155){f=1;q=2310-q} #find q in pd_2310_ l=0;h=239;m=120 while(q!=(k=pd_2310_[m])){ if(q=0;j--){ h=k/2;if(k-h-h)prime[ip++]=mi-pd_2310_[j];k=h if(ip==ma){i=ip;break} } } ibase=oib;scale=os;return 0 }