Help / Phrases / 8. A. Numbers & Counting

From J Wiki
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases


8A. Numbers & Counting

Constants such as π (1p1), half-π (1r2p1), integers expressed in base sixteen (16b2a for 42), complex numbers in terms of real and imaginary parts (2j3), and complex numbers in terms of magnitude and angle in degrees or radians (3ad90 and 3ar1) are commonly needed in programming.

Constant functions are also needed, as in odd=: 1:+2:*i.. The phrase c"r produces a constant function of rank r for any constant c (numeric, or other such as characters or boxed).

Many other constants, such as the reciprocal of π (1p_1), the multipliers used in converting from radians to degrees (180p_1) and from degrees to radians (1r180p1), the kth prime(p: k), and the list of prime factors of an integer (q: i) are commonly used.

The following phrases illustrate the production of various numbers, including such lists and tables as the binomial coefficients, various grids, Stirling numbers, primes and prime factors:

n0 =: pi=: 1p1 π
v1 =: PI=: 1p1"_ Other ranks are possible
n2 =: 2p1 1r2p1 1r6p1 List of multiples of π
n3 =: 2p_1 1r2p_1 1r6p_1 Multiples of reciprocal π
n4 =: 1x1 Euler&#146s number (2.71828 ...)
v5 =: 1x1 1x_1 _1x_1"0 List function of rank zero
m6 =: pitimes=: 1p1&* Like o. but infinite rank
m7 =: ln=: 1x1&^. Like ^. (natural log)
m8 =: ln=: 1x1"_ ^. ]    "
m9 =: [:^ 0j2p1&% * i. Roots of unity; e.g. m9 5
m10=: 1:=1:^m9 Roots of unity; e.g. m10 5
m11=: sbase=: %:@(2p1&%)* %&1x1 ^] Stirling's approximation, base term
m12=: scorr=: 1 1r12 1r288
       _139r51840 _571r2488320&p.@%
Stirling's approximation, correction
m13=: stirg =: sbase * scorr Stirling's approx for Γ(y) [[[Help/Phrases/References#1|AS[1]]] 6.1.37]
n14=: |1-(!10)%stirg 1+10 Relative error for !10
m15=: stirf =: ^@(1r12&%)
      *%:@(2p1&*) * %&1x1 ^ ]
Stirling's approx to !y [[[Help/Phrases/References#1|AS[1]]] 6.1.38]
n16=: |1-(!10)%stirf 10 Relative error for !10
m17=: even=: 2:*i. Even integers
m18=: odd=: >:@even Odd integers
m19=: odd=: 1:+2:*i. Odd integers
m20=: grid=: +`(*i.)/ grid b,s,n is From b step s for n
m21=: Ai=: >:@i. Augmented integers (1 to y)
m22=: +/\@($&1)    "   (but not negint arg)
m23=: Ei=: i.@>: Extended integers (0 to y)
m24=: Si=: Ei@+: - ] Symmetric integers (-y to y)
m25=: bc=: Ei ! ] Binomial coefficients
m26=: (0&,+,&0)^:([`1:)    "
m27=: bct=: Ei !/ Ei Pascal's triangle (bc table)
m28=: !/~@Ei    "
m29=: !~/~@Ei Pascal's triangle
m30=: (0&,+,&0)^:(i.`1:)    "
m31=: 1 1&([: +//. */)^:(i.`1:)    "
m32=: +/\@(|.!.0)^:(i.`($&1))    "
m33=: odometer=: #: i.@(*/) All #s in radix y (odometer)
m34=: >@,@{@(i.&.>"_)    "   Try m34 10 10
m35=: ,:@i.@0:`([: ,/ i.@{. ,."0
      2$:@}.)@.(*@#)
   "   Try m35 2 2 2
m36=: tt=: #:@i.@(2&^) Truth table of order y
m37=: odometer@($&2)    "
m38=: >@,@{@($&(<0 1))    "
m39=: ,:@i.`([: ,/ i.@2: ,."0 2
      $:@<:)@.*
   "
m40=: [:|:2&^($ #&0 1)"(0)2&^@i.@-    "
m41=: (i.@# = i.~) # ] Nub (all distinct items of) ~.
m42=: ~: # ]    "   Try m42 2 7 1 8 2 8
m43=: +./@(</\"1"_)@= # ]    "   m43 ;:'all in all'
m44=: ~.@(i.~) { ]    "
m45=: {./.~    "
m46=: ({.;#)/.~ Nub and count
m47=: #/.~    "   Try m47 2 7 1 8 2
m48=: +/"1@=    "   Try m48 ;: 'all in all'
m49=: 1: #. =    "
m50=: ifb=: # i.@# Index from boolean
d51=: bfi=: i.@[ e. ] Boolean from index; e.g. 0 d51 5 7
d52=: 1:`]`($&0@[)} Boolean from index
m53=: cfpv=: #;.1 Count from partition vector
m54=: pvfc=: [: ; {.&1&.> Partition vector from count
m55=: i.@(+/) e. 0&,@(+/\)    "
n56=: 2147483647=p: 105097564 The 105,097,564-th prime (0 origin)
m57=: lp=: p:@i. List first y primes
m58=: fotp=: (2&(_2:=-/\)#}:)@lp First of twin primes among first y
m59=: p:^:_1 Number of primes less than y
n60=: 2 2 2 3 5 -: q: 120 Prime factorization of 120
m61=: taut=: -: */@q: y = product of factors. Try m61 12345
m62=: dpe=: (~. ,: 1: #. =)@q: Distinct primes with exponents
m63=: |:@(({.,#)/.~)@q:    "
m64=: taut=: = */@(^/)@dpe y = product over powers. Try m64 120
m65=: = */ .^/@dpe    "
m66=: divisors=: /:~ @ , @ > @ (*/&.>/) @ ((^ i.@>:)&.>/) @ (__&q:) Divisors of y . Try m66 900
m67=: /:~@~.@(*/ .^"1 tt@#)@q:    "
m68=: /:~ @ , @: (*/&>) @ {@ (<@(1&,)@(*/\)/.~) @ q:   "
m69=: >:@i. ([ #~ 0: = |) ]    "
m70=: perfect=: +: = +/@divisors Perfect number test
m71=: 1: = #@q: Prime test
d72=: q:@[ -: -.&q: Relatively prime test
d73=: 1: = +.    "
m74=: totient=:* -.@%@~.&.q: Totient [[[Help/Phrases/References#3|GKP[3]]] 4.53]
m75=: */@(^/-(^<:)/)@dpe   "
m76=: [: +/ 1: = (+.i.)    "
m77=: [:/:~[:~.]|[:*:[:m50]m73[:i.] Quadratic residues
d78=: L=: [:{&_1 1|~e.(|*:@}.@i.)@] Legendre symbol
d79=: L * L~ Quadratic reciprocity
d80=: cfe=:<.@(([:%1:|])^:(i.@[`])) x terms of continued fraction expansion of y
m81=: rapprox=: (, % +.)&1"0 Rational approximation to y
m82=: [:>:[:m50]m72"0[:>:[:i.[:<:] Numbers prime to y
m83=: 3 : '<.@o.&.((10^y)&*) 1' y digits of π
m84=: [: +/ %@!@i. y digits of e
d85=: 4 : '-:@(+y&%)^:(>.2^.1>.x.-16)
      x:%:y'
x digits of the square root of integer y

The last three phrases illustrate operations on extended precision arguments. (0j_40 ": y formats y in exponential notation using 40 digits.)

   0j_40 ": m83 40x
3.1415926535897932384626433832795028841971e0
   0j_40 ": m84 40x
2.7182818284590452353602874713526624977572e0
   0j_40 ": 40 d85 2
1.4142135623730950488016887242096980785697e0

m83 exploits the fact that <.@o. produces extended integer results on extended integer arguments, and uses scaling to effect the desired computation.

m84 uses the reciprocal factorial power series to compute e, and produces extended precision results if given extended precision arguments.

d85 uses Newton's iteration to compute a rational approximation of the square root of integer y, accurate to x digits. The initial estimate is x:%:y, a rational approximation of the finite-precision square root of y. The number of iterations is the base-2 log of x-16, based on that the number of accurate digits doubles on every iteration and that the initial estimate has 16-digits of accuracy.



>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases