20171008, 22:02  #1 
∂^{2}ω=0
Sep 2002
República de California
3×7×13×43 Posts 
How to init an array in Posix bc?
Posix bc allows array variables and parameters, but I have only found oneelementatatime init to work, e.g. to declare and init a 2element array 'a':
a[2]; a[0]=1; a[1]=2; (Interestingly, adding a[2]=3 does not give an error and the resulting 3 element value are correct, so possibly bc autoexpands the array size in such cases, but I wouldn't count on this happening, i.e. best to init only within the declared array index range.) Especially for manyelement arrays it would be really nice to be able to combine declare/init in a fashion akin to C, e.g. 'a[2] = {1,2}', but I have found no syntax for the initializers which works, and websearch turns up only parroted manpages, none of which discuss arrayinit. Last fiddled with by ewmayer on 20171008 at 22:03 
20171008, 22:30  #2  
"Forget I exist"
Jul 2009
Dumbassville
2·5·839 Posts 
Quote:


20171009, 12:24  #3  
Sep 2002
Database er0rr
2^{2}×1,063 Posts 
Quote:
Your first use of a[2]; returns 0, as would the uninitialized a[100];. 

20171010, 00:48  #4 
Dec 2014
3·5·17 Posts 
GNU bc has an extension to allow passing arrays using "call by variable".
It looks like zero( *a[], n ) { for( i = 0; i < n; i++ ) a[i] = 0; } This is from reading the YACC grammar. I have not tried it. 
20171010, 06:31  #5  
∂^{2}ω=0
Sep 2002
República de California
26733_{8} Posts 
Quote:
Code:
MyMacBook:src ewmayer$ bc bc 1.06 Copyright 19911994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. a[2]; a[0]=1; a[1]=2; 0 <*** ewm: '0' here is return value of arraydeclaration statement *** a[2] 0 <*** Outofrange element prior to initing it a[2]=3 a[2] 3 a[0]*a[2] 3 a[1]*a[2] 6 a[2]*a[2] 9 

20171010, 08:46  #6  
Jun 2003
2×2,693 Posts 
Quote:
What happens if you omit your "declaration" a[2] altogether? Will it still work the same? 

20171010, 09:36  #7 
Sep 2002
Database er0rr
2^{2}×1,063 Posts 
Code:
bc 1.06.95 Copyright 19911994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. a[0]=1; a[1]=2; a[2] 0 a[2]=3 a[2] 3 a[0]*a[2] 3 a[1]*a[2] 6 a[2]*a[2] 9 Last fiddled with by paulunderwood on 20171010 at 09:53 
20171010, 23:46  #8 
∂^{2}ω=0
Sep 2002
República de California
3·7·13·43 Posts 
Not directly related  except in the sense that the arrayinit question came up while I was doing early prototyping  but a notterriblyoptimized but decently functional bcbased smallp sieve code follows:
Code:
/* returns 1 if p is a basez Fermat pseudoprime, 0 otherwise. */ define pprimef(p,base) { auto n,y,z,flag y = 1; n = p1; z = base; while(n) { flag = n%2; n /= 2; if(flag) y = (y*z)%p; z = (z*z)%p; if(!z) return(0); } return(y==1); } define isprp(p) { if(p == 2) return(1); if(p%2 == 0) return(0); /* Must only use inputs > the largest pprimefbase used below ... better, use that 341 is the smallest base2 Fermatpseudoprime: */ if(p <= 341) { return(pprimef(p,2)) } return(pprimef(p,2) && pprimef(p,3) && pprimef(p,5) && pprimef(p,7) && pprimef(p,11) && pprimef(p,13)); } define trailz(n) { auto i i = 0 while(n && (n%2 == 0)) { i += 1; n /= 2; } return(i) } define bits(n) { auto i; i = 0; while(n) { n /= 2; i += 1; } return(i) } define reverse(n,nbits) { auto tmp; tmp = 0; while(nbits) { tmp = 2*tmp + (n % 2); n /= 2; nbits = 1; } return(tmp); } define abs(n) { if(n < 0) return(n); return(n); } /* Find all prime factors of n which are <= max: */ define trialdiv(n,max) { auto i,p,ndiv if(max < 2) return(0); /* Only allow positive smallprime divisors */ if(abs(n) < 2) return(0); ndiv = n; i = trailz(ndiv); if(i) { print "Input ",n," has a small factor: 2^",i,"\n"; ndiv /= 2^i; } /* Now loop over odd primes <= max: */ p = 3; while(p <= max && p <= sqrt(ndiv)) { i = 0; while(ndiv%p == 0) { i++; ndiv/= p; } if(i) { print p^i*ndiv," has a small factor: ",p,"^",i,"\n"; if(isprp(ndiv)) { print "Remaining cofactor ",ndiv," is prime.\n"; break; } } /* find nextlarger prime (actually, base2 FermatPRP is fine in this context): */ while(1) { p += 2; if(pprimef(p,2)) { break; } } } return(0); } Now, something like a basic ECM implementation in bc to extend my factoring capability beyond TF  that might be an interesting side project for the coming year. 
20171011, 00:01  #9  
"Forget I exist"
Jul 2009
Dumbassville
20306_{8} Posts 
Quote:


20171011, 01:30  #10  
∂^{2}ω=0
Sep 2002
República de California
2DDB_{16} Posts 
Quote:
I wonder  given essentially the same level of underlying code optimization  which would be faster, pariviabrowser or bcrunlocallybased multiprecision? UPDATE: Moot in my case  tried a simple 'factor' job of a 64bit composite in the Pari/GP web app ... first nothing. Enabled JS for the website, that caused something to start loading, but completely locked up my browser (FF) with a "Transferring data from uni.bordeaux.fr..." in the lowerleft pageload infospace. Killed FF from a shell, reopened, retried, same lockingup occurred. One more application flunks the ewmayer "I just want it to !^*&$@% work" easeofuse criterion. Last fiddled with by ewmayer on 20171011 at 01:42 

20171011, 02:24  #11  
Dec 2014
FF_{16} Posts 
Quote:
gmp and most "advanced" packages do base 2^32 (or close) arithmetic. 

Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Array vs Hash Table vs Map for QS  Sam Kennedy  Programming  1  20121225 23:25 
Init.d Script for MPrime  CrashM  Software  0  20120518 01:01 
array of bits  Citrix  Programming  2  20050821 20:06 
Ubasic Array question  rn0dal  Programming  6  20040915 14:57 