Re: Tail call optimization

2025-02-14 Thread Alexander Burger
On Fri, Feb 14, 2025 at 07:30:59AM -0800, Lindsay Lawrence wrote:
> On Thu, Feb 13, 2025 at 9:49 PM Lindsay Lawrence <
> lawrence.lindsayj...@gmail.com> wrote:
> 
> 
> > A bit of searching came up with the paper with the haskell version
> >
> > https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
> >
> > Interestingly enough there is another version of PI in there, recursive,
> > based on the Gosper series,
> > that a faster... although it is based on a conjecture they haven't proven
> > in that paper.
> >
> > The picolisp version of that one is below ('tco' is a nice fit for it as
> > well!)
> >
> 
> There was an error in the variable scoping of the function I posted in
> previous email. Here is the corrected version
> 
> (de makePi (N)
>   (let (G 1 R 180 S 60 I 2)
>(tco (G R S I N)
>   (let
>  (U (* 3 (+ (* 3 I) 1) (+ (* 3 I) 2) )
>   Y (/ (+ (* G (- (* 27 I) 12)) (* 5 R) ) (* 5 S) ) )
>  (prin Y)
>  (if (or (not N) (gt0 N))
> (tc
>(* 10 G I (- (* 2 I) 1))
>(* 10 U (- (+ (* G (- (* 5 I) 2)) R) (* Y S) ) )
>(* S U)
>(+ I 1)
>(dec N) ) ) ) ) ))

Wow, that's cool! I'll measure it now :)


How about a coroutine version?

   (de pi (Flg)
  (if Flg
 (co 'pi
(yield 3)
(yield ".")
(let (G 60  R 13440  S 10080  I 3)
   (tco (G R S I)
  (let
 (U (* 3 (inc (* 3 I)) (+ (* 3 I) 2))
Y (/ (+ (* G (- (* 27 I) 12)) (* 5 R) ) (* 5 S)) )
 (yield Y)
 (tc
(* 10 G I (dec (* 2 I)))
(* 10 U (- (+ (* G (- (* 5 I) 2)) R) (* Y S )))
(* S U)
(inc I) ) ) ) ) )
 (co 'pi) ) )

   (de prinPi (N)
  (do N
 (prin (pi T)) )
  (pi)
  (prinl) )

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Tail call optimization

2025-02-14 Thread Lindsay Lawrence
>
> How about a coroutine version?
>
> Neat! You have options over what to do with the digits with the coroutine.
And negligible difference in performance, if it matters, either. I didn't
expect that.

/Lindsay

: (bench (out "pi-digits.1.txt" (makePi 1)))
3.150 sec
-> NIL
: (bench (out "pi-digits.1.txt" (prinPi 1)))
3.166 sec
-> NIL

: (bench (out "pi-digits.1.txt" (makePi 2)))
13.553 sec
-> NIL
: (bench (out "pi-digits.1.txt" (prinPi 2)))
13.811 sec
-> NIL

: (bench (out "pi-digits.1.txt" (prinPi 5)))
97.809 sec
-> NIL
: (bench (out "pi-digits.1.txt" (makePi 5)))
98.387 sec
-> NIL
:


Re: Tail call optimization

2025-02-14 Thread Alexander Burger
On Fri, Feb 14, 2025 at 05:21:46PM +0100, Alexander Burger wrote:
> Wow, that's cool! I'll measure it now :)

The old algorithm needed for 10 digits 8369 sec (02:19 h), the new
one (I use the coroutine version) just 726 sec. That's about 12 times as
fast!

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Unsubscribe

2025-02-14 Thread Heiko Henrich


___

Heiko Henrich 
Kirchenmusik, Jazz und Feldenkrais

Britzer Straße 58
12109 Berlin

01522 8776573
heiko.henr...@gmail.com




Re: Tail call optimization

2025-02-14 Thread Lindsay Lawrence
On Thu, Feb 13, 2025 at 9:49 PM Lindsay Lawrence <
lawrence.lindsayj...@gmail.com> wrote:


> A bit of searching came up with the paper with the haskell version
>
> https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
>
> Interestingly enough there is another version of PI in there, recursive,
> based on the Gosper series,
> that a faster... although it is based on a conjecture they haven't proven
> in that paper.
>
> The picolisp version of that one is below ('tco' is a nice fit for it as
> well!)
>

There was an error in the variable scoping of the function I posted in
previous email. Here is the corrected version

(de makePi (N)
  (let (G 1 R 180 S 60 I 2)
   (tco (G R S I N)
  (let
 (U (* 3 (+ (* 3 I) 1) (+ (* 3 I) 2) )
  Y (/ (+ (* G (- (* 27 I) 12)) (* 5 R) ) (* 5 S) ) )
 (prin Y)
 (if (or (not N) (gt0 N))
(tc
   (* 10 G I (- (* 2 I) 1))
   (* 10 U (- (+ (* G (- (* 5 I) 2)) R) (* Y S) ) )
   (* S U)
   (+ I 1)
   (dec N) ) ) ) ) ))

/Lindsay


Re: Tail call optimization

2025-02-14 Thread Lindsay Lawrence
On Fri, Feb 14, 2025 at 8:58 AM Alexander Burger 
wrote:

> The old algorithm needed for 10 digits 8369 sec (02:19 h), the new
> one (I use the coroutine version) just 726 sec. That's about 12 times as
> fast!
>


👍 👍 :)

/Lindsay