GBnums 4.2.0 04/04/08 12:03:07 -- initializing GB /* -- GBnums ist best seen with - police Lucinda Console 14 - window width >= 120 - window height >= 40 (right click in the window bar - Properties) The // comments are not part of the session The system prompt is '->' (may be changed with PROMPT= 'string ') */ /////////////////// // WEB HELP ///////////////////////// -> ?jacobi // --> opens web page - all about jacobi function nt jacobi argc=2 z_zz j = jacobi(n,m) -> url('jacobi') // what is the associated url ? http://en.wikipedia.org/wiki/Jacobi_symbol -> url('jacobi','http://www.disney.com'); // changes url //////////////////// // toplevel /////////////////// -> p = 666 // implicit (toplevel) variable declaration : mpz p ; $0 := 666 -> mpz n = 444444444444 // mpz declaration and init $1 := 444444444444 -> $0 + $1 //$i toplevel variables (use .hist to list) $2 := 444444445110 -> $ // $ is the value of the last evaluation $2 := 444444445110 -> -> n=10**100 // ** is the exponentiation operator $3 := 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -> n=10**100 ; // ";" ===> do not display result -> -> .var // list user variables ini googol 10**100 ini echo 666 ini user GB usr p 666 usr n 10**100 -> // usual C operators are here -> ++p // returns new value of p $4 := 667 -> p-- //returns current value then decrements $4 := 667 -> p // current value of p ? $5 := 666 -> // system function call -> nextprime(n) $6 := 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000267 -> nextprime(p) $7 := 673 -> r = rand(10**40) // random number < 10**40 $8 := 6986570984796482869151666948419433059605 -> decomp(d,r) // decomposition of r into matrix d : r=3**2 * 5 * 7 * 71 ... { 3, 2} { 5, 1} { 7, 1} { 71, 1} { 434437, 1} { 16718729, 1} { 691997263, 1} { 62152804861523, 1} -> d[6][0] $9 := 691997263 -> isprime($) // is it prime ? $10 := 1 // yes ///////////////////////////// // FERMAT /////////////////// -> powmod // function name without () to get prototype of info zmath powmod argc=3 z_zzz a=powmod(b,c,m); a = b**c mod(m) -> mpz n= randp(10**6) // a random prime $17 := 715889 -> powmod(2,n-1,n) // 2 **(n-1) (mod n) $18 := 1 // returns 1 because n is prime (Fermat) -> ////////////////// // MPF (real) values ////////////////////// -> x = 1.2345678 // implicit declare : mpf x ; $11 := 1.2345678 -> x $12 := 1.2345678 -> mpf y = x**2 // declare and init $13 := 1.524157653 -> fsqrt(y) $14 := 1.2345678 -> .pa // list system packages sys asm constant io str zmath bit nt qmath comb bag hash map vect complex mx solve poly fpoly contf tree fmath plot puzz usr ini -> .pa fmath // list contents of fmath package fmath acos argc=1 f_f acos(x) --> [0,PI] fmath agm argc=2 f_ff arithmetic-geometric mean(x,y) ... fmath tan argc=1 f_f fmath tanh argc=1 f_f fmath zeta argc=1 f_f zeta(s) = sigma[1...] 1/n**s -> x $15 := 1.2345678 -> y $16 := 1.524157653 -> precision() // ? current precision ? $17 := 10 -> precision(40) // want 40 digits internal precision $18 := 40 -> *dig* // list all 'digits' functions fmath fdigits argc=0 z_i mpf_display_digits = fdigits([digits]) puzz digitmul argc=1 z_z digitmul(12345) -> 120 puzz digitsum argc=1 z_z digitsum(12345) -> 15 -> fdigits(40) // want 40 digits mpf display $18 := 40 -> log(y) $19 := 0.4214418986312984809096363446679784370749 -> E**$19 $20 := 1.52415765279684 -> y $21 := 1.52415765279684 ///////////////////////// // BE CAREFUL ////////////////// -> 10.**2/6 // this is 10.**(1/3) $7 := 2.15443469 -> 10.**2 / 6 // this is 100. / 6 (floating point operations) $8 := 16.66666667 -> $7**3 $9 := 10.0 -> 10**2 / 6 // this is 100 / 6 (integer rounding) $10 := 16 /////////////////////// // CONSTANTS /////////////////// -> zeta(x) $22 := 4.857184931018234288031365886107000409981 -> .pa cons // list system global and constants constant UNDEF -1 ... constant PI 3.141592654 3.1415926535... constant E 2.718281828 log(E) -> 1. ... constant GAUSS 0.8346268417 1 / agm (1,SQRT_2) constant EPSILON 1.0e-40 ... constant I2 -1 -> zeta(2) // compute zeta(2) $23 := 1.644934066848226436472415166646025189219 -> PI**2 /6 $24 := 1.644934066848226436472415166646025189219 -> $24 == $23 // ? exact equal ? $30 := 0 // no -> $24 =# $23 // EPSILON compare - is |$24 - $23 | < EPSILON ? $31 := 1 // yes //////////////// // RATIONAL (Q) ////////// -> q= 1/3 $25 := 1/3 -> mpq r // declaration -- (1) r --> : : redefinition -> r = 42/666 $26 := 7/111 -> q + r $27 := 44/111 -> q*r $28 := 7/333 -> q**r // conversion to mpf $29 := 0.9330636533837107831842185104164709292183 -> .pa qmath // list qmath package contents qmath qabs argc=1 q_q qabs(-1/666) -> 1/666 ... qmath qsigma argc=3 q_Fii qs=qsigma(fun_q(n),nfrom,nto) qmath qsum argc=3 q_Fiii qs=qsum(fun_q(n),nfrom,nto[,step]) -> q = qnew(3*12+666,45**3) $30 := 26/3375 //////////// // VECTORS //////////////// -> mpz vz[] // empty mpz vector declaration -> mpz *vvz // same as mpz vvz[] -> mpz v1[] -> mpz *v2 -> string vs[10] -> mpz v3[] = {10,20,30} // declare and init $32 := 30 -> v3 { 10, 20, 30} -> uint v4[50] // vector of small ints (32 bits) >= 0 -> vrand(v4,100) // int with random values < 100 { 54, 26, 65, 5, 63, 19, 49, 96, 55, 23, 1, 61, 11, 55, 5, 67, 2, 68, 71, 12, 10, 28, 73, 82, 24, 10, 95, 6, 52, 1 7, 66, 17, 41, 63, 52, 3, 8, 44, 7, 65, 11, 92, 85, 42, 92, 0, 73, 58, 33, 5} -> .pa vect // list contents of vect package ... vect vpush argc=2 z_v* idx = vpush(v,item) vect vrand argc=2 v_vz vrand(v,range) v[i] := rand(range) vect vrandp argc=2 v_vz vrandp(v,range) v[i] := randp(range) vect vread argc=2 v_vsi dim = vread( /*into*/ v,filename) vect vreverse argc=1 v_v vreverse(v) ... -> vpush // vpush is a 2 args function, // which returns an mpz (z_) , takes a vector (v) as first arg, and anything (*) as second arg vect vpush argc=2 z_v* idx = vpush(v,item) -> vpush(v3,666) $33 := 3 // returs index of last pushed item -> v3 // show v3 { 10, 20, 30, 666} -> vsort(v4) // sorting { 0, 1, 2, 3, 5, 5, 5, 6, 7, 8, 10, 10, 11, 11, 12, 17, 17, 19, 23, 24, 26, 28, 33, 41, 42, 44, 49, 52, 52, 54, 55 , 55, 58, 61, 63, 63, 65, 65, 66, 67, 68, 71, 73, 73, 82, 85, 92, 92, 95, 96} -> vdim(v3,200) // new dimension for v3 $34 := 200 -> v3 { 10, 20, 30, 666, 0, ...} -> v3[10]++ // increment v3[10] $35 := 0 -> v3 { 10, 20, 30, 666, 0, 0, 0, 0, 0, 0, 1, 0, ...} -> v3[v4[2]] $36 := 30 -> v3[nextprime(2)] $2 := 666 -> v3[1000] // indexes are checked for overflow * v3[12] - idx:1000 * vector::get_idx : vector::bad index value // ERROR here //////////////////////////// // FUNCTIONS ////////////////////////// // function declaration - default type for args is mpz -> mpz nfact(n) {if(!n) return 1; return n * nfact(n-1);} -- (1) function: usr.nfact -> nfact // function name to see it usr nfact argc=1 mpz nfact(n) {if(!n) return 1; return n * nfact(n-1);}; -> nfact(42)//our function $3 := 1405006117752879898543142606244511569936384000000000 -> fact(42)// system function $3 := 1405006117752879898543142606244511569936384000000000 -> v3 { 10, 20, 30, 42, 0, 0, 0, 0, 0, 0, 1, 0} -> vmap(v3,nfact) // mapping a function onto a vector { 3628800, 2432902008176640000, 265252859812191058636308480000000, 14050061177528798985431426062445115699363840000 00000, 1, 1, 1, 1, 1, 1, 1, 1} -> mpq foo(mpq q, n) {return 1/(q**n -1);} -- (1) function: usr.foo -> foo(1/3,4) $5 := -81/80 -> foo(1/666,3) $6 := -295408296/295408295 -> mpf gee(mpz n) {static mpf u; return u**n / fact(n);} // u**n / n! -- (1) function: usr.gee -> gee.u = 10 // static variable u in 'package' gee $18 := 10. -> gee(3) $19 := 166.6666667 -> fsigma fmath fsigma argc=2 f_Fiii fs=fsigma(fun_f(n),nfrom[,nto|INFINITY[,step|1]]) -> fsigma(gee,0) $26 := 2.718281828 // sigma(0,infinity) 1/n! = E -> gee.u = 10 $27 := 10. -> fsigma(gee,0) // sigma (0,infinity) 10**n/n! $28 := 22026.46579 -> E**10 $29 := 22026.46579 -> $28 =# $29 $30 := 1 //////////////////////////// // prime factors /////////////////////////////////////// -> *fact* zmath fact argc=1 z_i n! zmath factmod argc=2 z_iz a=fatcmod(b,m): a = b! mod(m) nt factor argc=1 z_zi f=factor(n[,verbose=0|1|2]) -- set P := n/f nt factorp argc=1 z_ziii f=factorp(n[,0|1|2]) (pollard-rho) - set P := n/f nt factorq argc=1 z_zi f=factorq(n[,0|1|2]) (quad. sieve) - set P := n/f nt factorsmall argc=1 z_zi f=factorsmall(n[,numprimes]) (trial div) - set P := n/f nt factors argc=2 v_vzi factors(/*into*/ V,n [verbose=0|1|2]) -see also decomp nt factornum argc=1 z_zi numprimefactors_with_repeat = factornum(n [,cut]) nt factorsum argc=1 z_zi sumofprimefactors_with_repeat = factorsum(n) -> n= randp(10**20)**2 * randp(10**20) $1 := 80816174777434912109299687073417419734253283798454375014597 -> factor(n) // wants a prime factor f of n $2 := 89450673642760296077 -> P // factor sets the global P to n/f $3 := 903471952600278506106704914960366100761 -> n @ $2 // exact divide n by f $3 := 903471952600278506106704914960366100761 -> decomp(d,n) // decomp n into a matrix 'd' of {factor,exponent} { 89450673642760296077, 1} { 30057810176396392069, 2} -> factornum(n) $4 := 3 -> do { f= factor(n); println(f) ;n @= f ;} while(f != 1) 89450673642760296077 30057810176396392069 30057810176396392069 1 $5 := 1 -> n = 2**3 * 7**2 * 101 $6 := 39592 -> divisors(dlist,n) // all divisors of n into list dlist { 1, 2, 4, 8, 7, 14, 28, 56, 49, 98, 196, 392, 101, 202, 404, 808, 707, 1414, 2828, 5656, 4949, 9898, 19796, 39592} /////////////////////////////// // lists operations ////////////////////// -> .pa vect ... vect vrotl argc=1 v_vi vrotl(v [,n|1]) -- rotate left n times vect vrotr argc=1 v_vi vrotr(v [,n|1]) -- rotate right n times vect vshiftl argc=2 v_v* vshiftl(v,rightpad) vect vshiftr argc=2 v_v* vshiftr(v,leftpad) vect vsum argc=1 *_v vsum(v) = SUM[0..dim-1] v[i] ... vect vuniq argc=1 v_v vuniq(v) -> suppress duplicates vect vxmul argc=3 v_vvv vxmul(v,a,b); -- v[i] := a[i]*b[i] -> mpz l[12] -> vrand(v,100) {} -> vrand(l,100) { 21, 70, 16, 52, 54, 26, 65, 5, 63, 19, 49, 96} -> vreverse(l) // reverse it { 96, 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 21} -> vshiftl(l,666) // left shift with padding { 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 21, 666} -> vshiftr(l,42) // right shift with padding { 42, 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 21} -> vset vect vset argc=2 v_vvii vset(v1,v2 [,lg [,ifrom]]) : v1 := v2 (with conversion) -> vset(v,l,6,2) // extract a sub-list from index 2, length 6 { 19, 63, 5, 65, 26, 54} -> l // STACK operartions { 42, 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 21} -> vpop(l) // POP last item $0 := 21 -> l { 42, 49, 19, 63, 5, 65, 26, 54, 52, 16, 70} -> vpush(l,777) // PUSH an item $1 := 11 -> l { 42, 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 777} -> vcdr(l,l) { 49, 19, 63, 5, 65, 26, 54, 52, 16, 70, 777} -> vsort(l) // SORT list { 5, 16, 19, 26, 49, 52, 54, 63, 65, 70, 777} -> vbsearch(l,666) // binary search $2 := -1 // not found -> vbsearch(l,49) $3 := 4 // found. returns index -> l[4] $4 := 49 -> v { 63, 65, 70, 999} -> l { 5, 16, 19, 26, 49, 52, 54, 63, 65, 70, 777} -> vunion(v,v,l) // SET operations { 5, 16, 19, 26, 49, 52, 54, 63, 65, 70, 777, 999} //////////////////// // INTERRUPTIONS ///////////////////////// -> while(1) {i++;} // type CTRL-C to stop this endless loop *** Z-machine : error 666 : stopped - 1.609 sec. -> while(1) {i++;} // type CTRL-PAUSE to interrupt [0003] (1) break (.q: exit inspector & continue) BREAK(1) -> i // we are in the inspector - what is the value of i ? $0 := 12190938 BREAK(0) -> .q // quit the inspector // CTRL-C to return to toplevel *** Z-machine : error 666 : stopped - 10.438 sec. ////////////////////////// // SHOW MY ENVIRONMENT /////////////////////////// -> .env GBnums 4.2.5 [RUNS] = 2465 constant EPSILON 1.0e-10 sys DEBUG 7 see system.h io PATH c:\gbnums io EDITOR "c:\Program Files\Textpad 4\textpad.exe" preferred editor path io PROMPT -> io TAB \t print separator : usage : TAB = ';' TAB ='\t' ... sys TRACE 0 0=none 1=run 2=step/step ini user GB sys VERBOSE 1 0=none 1=medium 2=all io VFORMAT 0 vector output format : see system.h display Z-Q digits(mpz/mpq) 0 (0=all) display F-digits(mpf) 10 vector display items (0=all) 0 internal F-digits(mpf) 10 -> precision(20) // changing precision .... $11 := 20 -> EPSILON // does change EPSILON $12 := 1.0e-20 ////////////////////////////// // end of session ////////////////////// -> .q really quit? (y/n) : y