#!/usr/local/bin/bc -l primes.bc ### Primes-Other.BC - Extra functions to go along with Primes.BC # Both Funcs.BC and Primes.BC are required to use functions herein # Returns 2, 3, or number of form 6n[+-]1 define aq(x) { if(x<0)return(-aq(-x)) if(x<3)return(x+1) x-=3;x+=int(x/2) return(x+x+5) } # Inverse of the above define iaq(x) { if(x<0)return(-iaq(-x)) if(x<4)return(x-1) return((remainder(x+3,6)+x+x)/6+1) } # Returns 2, 3, 5 or number of form 30n[+-]{1,7,11,13} define aq30(x) { auto os, e, r, rh, s os=scale;scale=0;x/=1 if(x<0){x=-aq30(-x);scale=os;return(x)} if(x<4){x=x+1+x/3 ;scale=os;return(x)} x-=3 ; e=x/8 r=x%8 ; rh=r/4 s=1-2*rh ; r=s*r+7*rh scale=os;return( 3*A*(e+rh)-s*(r*(r-7)-1) ) } # Inverse of the above define iaq30(x) { auto os, e, r os=scale;scale=0;x/=1 if(x<0){x=-iaq30(-x);scale=os;return(x)} if(x<7){x=x-1-x/5 ;scale=os;return(x)} e=x/30;r=x%30 r=r/6+(r-2)/7 scale=os;return(8*e+r +3) } # Cyrek's Approximation to the Prime Counting Function pi(x) define aprimepi(x) { auto la,b,lx,k,oib; if(x<=0)return 0 if(x 2+3+5*2 = 15 define sum_of_factors(x) { auto i,c,fp[]; if(x<0)return sum_of_factors(-x)-1; if(x==0||x==1)return 0; .=fac_store(fp[],x) for(i=0;fp[i];i++)c+=fp[i]*fp[++i] return c; } # As above but with no splitting of powers into multiplies # . e.g. 150 = 2*3*5^2 -> 2+3+5^2 = 30 define sum_of_factor_terms(x) { auto i,c,fp[]; if(x<0)return sum_of_factor_terms(-x)-1; if(x==0||x==1)return 0; .=fac_store(fp[],x) for(i=0;fp[i];i++)c+=fp[i]^fp[++i] return c; } # Raise the powers of the prime factors to the # power of their primes and multiply # . e.g. 150 = 2*3*5^2 -> 1^2*1^3*2^5 = 1*1*32 = 32 define factor_invert(x) { auto i,c,fp[]; if(x<0)return factor_invert(-x)+1; if(x==0||x==1)return 0; .=fac_store(fp[],x) c=1;for(i=0;fp[i];i+=2)c*=fp[i+1]^fp[i] return c; }