In http://www.jsoftware.com/jwiki/Essays/Repeated_Squaring Hui described
this solution already.

powG=: 4 : 0"0 
  if. 0=y do. 1 return. end.
  if. 2|y do. x * *: x powG -:<:y  else. *: x powG -:y  end.
)
powG1=: 4 : '(*:@]`(x**:@])@.[)/,1x,~2|<[EMAIL PROTECTED]:^:a:,y'"0

powH=: 4 : '*/ *~^:(I.|.#:y) x'

   5 ts '3x powG 100000'
0.38914173 332672
   5 ts '3x powG1 100000'
0.38533968 343680
   5 ts '3x powH 100000'
0.43796215 594048

   3x (powG-:powH)100000
1


R.E. Boss


> -----Oorspronkelijk bericht-----
> Van: [EMAIL PROTECTED] [mailto:programming-
> [EMAIL PROTECTED] Namens Arie Groeneveld
> Verzonden: zondag 20 juli 2008 18:35
> Aan: Programming forum
> Onderwerp: [Jprogramming] Speeding up dyad ^ for x: results
> 
> I don't know if this already passed the forum: how to speed up
> exponentiation x^y with y>:0 for integer arguments giving a x: result;
> e.g. if you have to change to x: in case:
> 
> y > ([:<.308*%@(10&^.))"0, x
> 
> 
>    ([:<.308*%@(10&^.))"0, 2
> 1023
> 
>    2^1023
> 8.988465674e307
> 
>    2^1024
> _
> 
>    2x^1024
> 17976931348................
> 
> 
>  From existing algorithms:
> 
> pow=: 4 : 0"0
>   if. 0=y do. 1 return. end.
>   if. 2|y do. x * *: x pow -:<:y  else. *: x pow -:y  end.
> )
> 
> or:
> powi=: 4 : '(*:@]`(x**:@])@.[)/,1x,~2|<[EMAIL PROTECTED]:^:a:,y'"0
> 
> or:
> powt=: ((*:@[$:-:@])`([**:@[$:-:@<:@])@.(2|]))`1:@.(0=])"0
> 
> and:
> pow2=: (*/@:^2x^I.@|[EMAIL PROTECTED]:@])"0
> 
> >From  ~help/dictionary/cwhile.htm :
> 
> exp =: 4 : 0"0
>  z=.1
>  a=.x
>  n=.y
>  while. n do.
>   if. 2|n do. z=.z*a end.
>   a=.*:a
>   n=.<.-:n
>  end.
>  z
> )
> 
>    ts '3x ^ 100000'
> 3.138055 362560
> 
>    ts '3x pow 100000'
> 1.298968 166464
> 
>    ts '3 powi 100000'
> 1.299312 171776
> 
>    ts '3x powt 100000'
> 3.138818 209152
> 
>    ts '3 pow2 100000'
> 3.716827 576896
> 
>    ts '3x exp 100000'
> 3.154187 231680
> 
> 
> pow and powi are reasonable candidates. Is there more to gain?
> 
> 
> =@@i
> 
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to