![]() |
![]() |
#1 |
∂2ω=0
Sep 2002
República de California
3×7×13×43 Posts |
![]()
Posix bc allows array variables and parameters, but I have only found one-element-at-a-time init to work, e.g. to declare and init a 2-element 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 auto-expands 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 many-element 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 array-init. Last fiddled with by ewmayer on 2017-10-08 at 22:03 |
![]() |
![]() |
![]() |
#2 | |
"Forget I exist"
Jul 2009
Dumbassville
2·5·839 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
#3 | |
Sep 2002
Database er0rr
22×1,063 Posts |
![]() Quote:
Your first use of a[2]; returns 0, as would the uninitialized a[100];. |
|
![]() |
![]() |
![]() |
#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. |
![]() |
![]() |
![]() |
#5 | |
∂2ω=0
Sep 2002
República de California
267338 Posts |
![]() Quote:
Code:
My-MacBook:src ewmayer$ bc bc 1.06 Copyright 1991-1994, 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 array-declaration statement *** a[2] 0 <*** Out-of-range 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 |
|
![]() |
![]() |
![]() |
#6 | |
Jun 2003
2×2,693 Posts |
![]() Quote:
What happens if you omit your "declaration" a[2] altogether? Will it still work the same? |
|
![]() |
![]() |
![]() |
#7 |
Sep 2002
Database er0rr
22×1,063 Posts |
![]() Code:
bc 1.06.95 Copyright 1991-1994, 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 2017-10-10 at 09:53 |
![]() |
![]() |
![]() |
#8 |
∂2ω=0
Sep 2002
República de California
3·7·13·43 Posts |
![]()
Not directly related - except in the sense that the array-init question came up while I was doing early prototyping - but a not-terribly-optimized but decently functional bc-based small-p sieve code follows:
Code:
/* returns 1 if p is a base-z Fermat pseudoprime, 0 otherwise. */ define pprimef(p,base) { auto n,y,z,flag y = 1; n = p-1; 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 pprimef-base used below ... better, use that 341 is the smallest base-2 Fermat-pseudoprime: */ 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 small-prime 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 next-larger prime (actually, base-2 Fermat-PRP 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. |
![]() |
![]() |
![]() |
#9 | |
"Forget I exist"
Jul 2009
Dumbassville
203068 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
#10 | |
∂2ω=0
Sep 2002
República de California
2DDB16 Posts |
![]() Quote:
I wonder - given essentially the same level of underlying code optimization - which would be faster, pari-via-browser or bc-run-locally-based multiprecision? UPDATE: Moot in my case - tried a simple 'factor' job of a 64-bit 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 lower-left page-load infospace. Killed FF from a shell, reopened, retried, same locking-up occurred. One more application flunks the ewmayer "I just want it to !^*&$@% work" ease-of-use criterion. Last fiddled with by ewmayer on 2017-10-11 at 01:42 |
|
![]() |
![]() |
![]() |
#11 | |
Dec 2014
FF16 Posts |
![]() Quote:
gmp and most "advanced" packages do base 2^32 (or close) arithmetic. |
|
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Array vs Hash Table vs Map for QS | Sam Kennedy | Programming | 1 | 2012-12-25 23:25 |
Init.d Script for MPrime | CrashM | Software | 0 | 2012-05-18 01:01 |
array of bits | Citrix | Programming | 2 | 2005-08-21 20:06 |
Ubasic Array question | rn0dal | Programming | 6 | 2004-09-15 14:57 |