Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"Open the pod bay doors, HAL." -- Dave Bowman, 2001


devel / comp.lang.forth / Re: recursion is silly

SubjectAuthor
* recursion is sillynone
+* Re: recursion is sillyLIT
|+- Re: recursion is sillyJulieta Shem
|`* Re: recursion is sillyGeorge Neuner
| +* Re: recursion is sillyKaz Kylheku
| |`* Re: recursion is sillyGeorge Neuner
| | `* Re: recursion is sillyKaz Kylheku
| |  +* Re: recursion is sillyPaul Rubin
| |  |+* Re: recursion is sillyJulieta Shem
| |  ||`* Re: recursion is sillyPaul Rubin
| |  || `* Re: recursion is sillyJulieta Shem
| |  ||  `- Re: recursion is sillyPaul Rubin
| |  |+* Re: recursion is sillyAnton Ertl
| |  ||+* Re: recursion is sillyAnton Ertl
| |  |||+- Re: recursion is sillydxf
| |  |||`* Quicksort (was: recursion is silly)Anton Ertl
| |  ||| +* Case for loops - not vice versadxf
| |  ||| |`* Re: Case for loops - not vice versaAnton Ertl
| |  ||| | +* Re: Case for loops - not vice versamhx
| |  ||| | |`* Re: Case for loops - not vice versaAnton Ertl
| |  ||| | | `- Re: Case for loops - not vice versadxf
| |  ||| | `- Re: Case for loops - not vice versadxf
| |  ||| `- Re: QuicksortGerry Jackson
| |  ||`* Re: recursion is sillynone
| |  || `* Re: recursion is sillyAnton Ertl
| |  ||  `- Re: recursion is sillynone
| |  |`- Re: recursion is sillynone
| |  +- Re: recursion is sillyGeorge Neuner
| |  `* Re: recursion is sillynone
| |   `* Re: recursion is sillyPaul Rubin
| |    `* Re: recursion is sillynone
| |     +* Re: recursion is sillymhx
| |     |+* Re: recursion is sillyKaz Kylheku
| |     ||+* Re: recursion is sillyKaz Kylheku
| |     |||`- Re: recursion is sillynone
| |     ||`* Re: recursion is sillynone
| |     || `* Re: recursion is sillyKaz Kylheku
| |     ||  `* Re: recursion is sillymhx
| |     ||   `* Re: recursion is sillyKaz Kylheku
| |     ||    `* Re: recursion is sillymhx
| |     ||     `- Re: recursion is sillyAnton Ertl
| |     |`- Re: recursion is sillyPaul Rubin
| |     `* Re: recursion is sillyPaul Rubin
| |      +* Re: recursion is sillyJames Brakefield
| |      |`- Re: recursion is sillyPaul Rubin
| |      `- Re: recursion is sillynone
| +- Re: recursion is sillynone
| `* Re: recursion is sillydxf
|  `- Re: recursion is sillyGeorge Neuner
+* Re: recursion is sillyAnton Ertl
|+- Re: recursion is sillyLIT
|`- Re: recursion is sillynone
+- Re: recursion is sillyJulieta Shem
+* Re: recursion is sillyPaul Rubin
|`* Re: recursion is sillyPaul Rubin
| `* Re: recursion is sillyPaul Rubin
|  +* Re: recursion is sillymhx
|  |+- Re: recursion is sillyPaul Rubin
|  |+* Re: recursion is sillyPaul Rubin
|  ||`* Re: recursion is sillynone
|  || +* Re: recursion is sillyPaul Rubin
|  || |+- Re: recursion is sillynone
|  || |`- Re: recursion is sillynone
|  || `* Re: recursion is sillymhx
|  ||  +- Re: recursion is sillynone
|  ||  `* Re: recursion is sillynone
|  ||   `* Re: recursion is sillyPaul Rubin
|  ||    `* Re: recursion is sillynone
|  ||     `- Re: recursion is sillyPaul Rubin
|  |`- Re: recursion is sillynone
|  `- Re: recursion is sillynone
`- Re: recursion is sillyminforth

Pages:123
Re: recursion is silly

<nnd$011e5e61$7cbf101d@58206efa1e451f88>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25870&group=comp.lang.forth#25870

  copy link   Newsgroups: comp.lang.forth
Newsgroups: comp.lang.forth
Subject: Re: recursion is silly
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <2023Dec30.084142@mips.complang.tuwien.ac.at> <nnd$4e0464a2$72397d2b@98ccb8d7c74a6c64> <2023Dec31.170120@mips.complang.tuwien.ac.at>
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$011e5e61$7cbf101d@58206efa1e451f88>
Organization: KPN B.V.
Date: Mon, 01 Jan 2024 13:33:58 +0100
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe006.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 38
Injection-Date: Mon, 01 Jan 2024 13:33:58 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2115
 by: none - Mon, 1 Jan 2024 12:33 UTC

In article <2023Dec31.170120@mips.complang.tuwien.ac.at>,
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>albert@cherry.(none) (albert) writes:
>>In article <2023Dec30.084142@mips.complang.tuwien.ac.at>,
>>Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
>>>
>>>\ SORTM (best-performing)
>>>: sort2 ( al ah -- )
>>> begin
>>> over cell+ over u< while
>>> partition recurse
>>> repeat
>>> 2drop ;
>>
>>I hate it if one uses U< in comparing numbers that hapenns
>>to be positive.
>
>What makes you think that this code compares numbers with U<? It
>actually compares two addresses, and I certainly don't want my sorting
>programs to fail when the sorted array straddles the addresses
>2147483647 and 2147483648 on a 32-bit system. Note that an "a" prefix
>typically does not indicate a signed number.

Sorry, my bad. My qsort's all works with indices.
That remark is out of place.

>
>Followups set to comp.lang.forth
>
>- anton

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$6f8d4fb1$5509f70d@46490c8c7354685d>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25871&group=comp.lang.forth#25871

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <20231227150908.647@kylheku.com> <1gjroi9kbq8vss6r8nond3s0nj6p0ob6t0@4ax.com> <20231228140434.720@kylheku.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
Organization: KPN B.V.
Date: Mon, 01 Jan 2024 14:01:10 +0100
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!tr2.iad1.usenetexpress.com!feeder.usenetexpress.com!tr1.eu1.usenetexpress.com!2001:67c:174:101:2:67:202:4.MISMATCH!feed.abavia.com!abe004.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 68
Injection-Date: Mon, 01 Jan 2024 14:01:10 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
 by: none - Mon, 1 Jan 2024 13:01 UTC

In article <20231228140434.720@kylheku.com>,
Kaz Kylheku <433-929-6894@kylheku.com> wrote:
>On 2023-12-28, George Neuner <gneuner2@comcast.net> wrote:
>> On Wed, 27 Dec 2023 23:12:07 -0000 (UTC), Kaz Kylheku
>><433-929-6894@kylheku.com> wrote:
>>
>>>On 2023-12-27, George Neuner <gneuner2@comcast.net> wrote:
>>>> On Wed, 27 Dec 2023 15:57:48 +0000, zbigniew2011@gmail.com (LIT)
>>>> wrote:
>>>>
>>>>>> Recursion is silly (if you can avoid it).
>>>>>
>>>>>Not really.
>>>>>
>>>>>Recursion is elegant — but actually isn't any
>>>>>more efficient than iteration, and of course
>>>>>it requires stack space.
>>>>
>>>> Not necessarily. Scheme compilers actually are *required* to turn
>>>> self recursion into a loop (except in the case of a supported "debug"
>>>> mode).
>>>
>>>Careful there: not all self-recursion is tail recursion!
>>
>> Oops, you're right. I should have said Scheme is required to turn
>> *tail-recursion* into a loop.
>>
>> However, it is true that any recursion can be turned into a loop, and
>
>Can it, by an algorithm? That's a theoretical question that's above my pay grade.
>
>(Or, well, below; if I were a CS academic, I'd make less.)
>
>It doesn't seem obvious to me. Consider a graph traversal (like garbage
>collection marking). When you visit N pointers of a graph node, only the
>last visit can be a tail call. The first N-1 have to return to that
>node.
>
>There is a mildly famous (in some circles) pointer-reversal trick that
>has been used to implement a stackless traversal for garbage collection.
>But it's not a simple code transformation; it mutates the nodes in order
>to, effectively, create a return stack within the graph itself.
>
>That could not possibly be a valid automatic compiler optimization.

Note that in the example that started this threat (the count change
program) there was a double recursion, that cannot be handled
readily with a tail recursion optimisation.
On the other hand the alternative algorithm was easier to understand
even to lispers who are thoroughly familiar with recursion.
It runs fast without the need for external optimisation like a
memoization.

And may I stress it:
This is my home brew compiler. It is a couple of thousand lines in
assembler. The compiler is built by a single call to the fasm
assembler, that takes less that 10 mS. No external library.
Two nested loops, that is all. As small as the original algorithm.

>
>--
>TXRg ProgramminLanguage: http://nongnu.org/txr
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$6a9a3264$3d0c8364@6e9f90d04f28c6ef>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25873&group=comp.lang.forth#25873

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <8af29958c24b1596a65d3d7f927f6211@news.novabbs.com> <nnd$3aa91e3a$4f77b356@ea42f28632e84cdd> <87jzowirtb.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$6a9a3264$3d0c8364@6e9f90d04f28c6ef>
Organization: KPN B.V.
Date: Mon, 01 Jan 2024 15:45:59 +0100
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 33
Injection-Date: Mon, 01 Jan 2024 15:45:59 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2083
 by: none - Mon, 1 Jan 2024 14:45 UTC

In article <87jzowirtb.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>albert@cherry.(none) (albert) writes:
>> Rubin's program also can be run with an older version of Guile (2.0.13).
>> My guess is that Rubin made a fresh start each time
>
>Yes, I re-ran the program for each value, to get the timing.
>
>> with the mysterious ?singleton call.
>
>singleton? just checks whether a list has exactly one element. It is
>used to detect the case where only one coin is available to make change
>with. In that case, there are either 0 ways or 1 way to make change,
>depending on whether the coin denomination divides the amount to be
>changed.

Next guess.
With
(set! cc (memoize cc))
you overwrite the `cc to henceforth to use the memoizer.

That means that the second calculation of the same results
is immediate and that larger results benefit from the
smaller results that are calculated before them.
True?

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<87v88amgc8.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25879&group=comp.lang.forth#25879

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 03 Jan 2024 10:17:43 -0800
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <87v88amgc8.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<8af29958c24b1596a65d3d7f927f6211@news.novabbs.com>
<nnd$3aa91e3a$4f77b356@ea42f28632e84cdd>
<87jzowirtb.fsf@nightsong.com>
<nnd$6a9a3264$3d0c8364@6e9f90d04f28c6ef>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="683b36643af16cd74f5304cb9e325f28";
logging-data="3465797"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BOA6xnWZZN/07g09cxqGB"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:zgh6K0AwZrGPOsyxmGY9ZxKzyXg=
sha1:Rvt34MS6Y0TC0ZpQWEegEF/uNpY=
 by: Paul Rubin - Wed, 3 Jan 2024 18:17 UTC

albert@cherry.(none) (albert) writes:
> you overwrite the `cc to henceforth to use the memoizer.
>
> That means that the second calculation of the same results
> is immediate and that larger results benefit from the
> smaller results that are calculated before them.
> True?

Yes, that is exactly right, sorry about the slow response. The Python
equivalent (untested) is:

def memoize(f):
def m(*args):
if args in memo: return memo[args]
memo[args] = f(*args)
return memo[args]
return m

@memoize
def cc(n, coins): ...

There is an aspect of the Scheme version that confuses me slightly but I
will ask some Scheme experts (I'm not one myself).

Re: recursion is silly

<87ttnu54qa.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25880&group=comp.lang.forth#25880

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 03 Jan 2024 16:20:29 -0800
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87ttnu54qa.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231227150908.647@kylheku.com>
<1gjroi9kbq8vss6r8nond3s0nj6p0ob6t0@4ax.com>
<20231228140434.720@kylheku.com>
<nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91a630b595e6e5bef44e319b69d624fa";
logging-data="3559033"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MtTiA367H9jcvLgA5aEfu"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:PHgcFDl5m1wwGAVlslgB59dzO5E=
sha1:SJxmo0kc4tM5OamX3y/arEPpqlc=
 by: Paul Rubin - Thu, 4 Jan 2024 00:20 UTC

albert@cherry.(none) (albert) writes:
> On the other hand the alternative algorithm was easier to understand
> even to lispers who are thoroughly familiar with recursion.
> It runs fast without the need for external optimisation like a
> memoization.

I don't think I examined it, but is that the one that iteratively fills
the lookup table before doing the requested value?

I found in the memoizing Scheme version, after computing (cc 1000000),
the memo table held 670004 entries, which is much less than the 5
million that would be used by precomputing all the earlier entries.

When I removed the optimization (I originally hadn't thought of it as
one) of quickly checking divisibility when only one coin denomination
was available, the table size went up to 2470005 entries, still half
of what it would have been from precomputing everything.

Re: recursion is silly

<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25881&group=comp.lang.forth#25881

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <20231228140434.720@kylheku.com> <nnd$6f8d4fb1$5509f70d@46490c8c7354685d> <87ttnu54qa.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
Organization: KPN B.V.
Date: Thu, 04 Jan 2024 10:52:28 +0100
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 38
Injection-Date: Thu, 04 Jan 2024 10:52:28 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2681
 by: none - Thu, 4 Jan 2024 09:52 UTC

In article <87ttnu54qa.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>albert@cherry.(none) (albert) writes:
>> On the other hand the alternative algorithm was easier to understand
>> even to lispers who are thoroughly familiar with recursion.
>> It runs fast without the need for external optimisation like a
>> memoization.
>
>I don't think I examined it, but is that the one that iteratively fills
>the lookup table before doing the requested value?
>
>I found in the memoizing Scheme version, after computing (cc 1000000),
>the memo table held 670004 entries, which is much less than the 5
>million that would be used by precomputing all the earlier entries.
>
>When I removed the optimization (I originally hadn't thought of it as
>one) of quickly checking divisibility when only one coin denomination
>was available, the table size went up to 2470005 entries, still half
>of what it would have been from precomputing everything.

Once you do memoization in Scheme, it is important that you mention
the order of nominations (increasing or decreasing).
The minimum should be N/50 + N/25 + N/10 + N/5 + N as far a I can see.
(In the order of increasion nominations)
I can't understand that there can be less that a million entries, unless ..
Can the memoization "see" that once cc(N-5,5) is known,
cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
in the iterative version),
and that cc(N,5) becomes cc(N,4)+cc(N-5,5) . That is chill.
(The iterative version also saves some 50% of the time going 50 -> 1)

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25882&group=comp.lang.forth#25882

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: mhx@iae.nl (mhx)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 4 Jan 2024 11:58:58 +0000
Organization: novaBBS
Message-ID: <62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <20231228140434.720@kylheku.com> <nnd$6f8d4fb1$5509f70d@46490c8c7354685d> <87ttnu54qa.fsf@nightsong.com> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="2237290"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$ejKDtyUh9ASDFxDh6z0cXuZDReJcE8mXYIEEgmQmVg7wr67CMJUdS
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 463cbf1a76c808942982a163321348c75477c065
 by: mhx - Thu, 4 Jan 2024 11:58 UTC

> Can the memoization "see" that once cc(N-5,5) is known,
> cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
[..]

I never used naive memoization much because the table size becomes
impractical for real-world problems. If it is known how to compress
N-dim tables (in hopefully a simple way), I'd be very interested
to know how it is done (even when N=1).

-marcel

Re: Quicksort

<un6hld$3m8ts$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25884&group=comp.lang.forth#25884

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: do-not-use@swldwa.uk (Gerry Jackson)
Newsgroups: comp.lang.forth
Subject: Re: Quicksort
Date: Thu, 4 Jan 2024 15:08:29 +0000
Organization: A noiseless patient Spider
Lines: 96
Message-ID: <un6hld$3m8ts$1@dont-email.me>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com> <20231227150908.647@kylheku.com>
<1gjroi9kbq8vss6r8nond3s0nj6p0ob6t0@4ax.com> <20231228140434.720@kylheku.com>
<87le9e7wq8.fsf@nightsong.com> <2023Dec30.084142@mips.complang.tuwien.ac.at>
<2023Dec30.095714@mips.complang.tuwien.ac.at>
<2023Dec30.165452@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 4 Jan 2024 15:08:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ccae677fd5c82223a11789fe95d32e2f";
logging-data="3875772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1WiUxpahn65zpUPRgMxGMbm3Lp7dI1WY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SVUHAbG3YIpI15q456LuvuPDpjA=
In-Reply-To: <2023Dec30.165452@mips.complang.tuwien.ac.at>
Content-Language: en-GB
 by: Gerry Jackson - Thu, 4 Jan 2024 15:08 UTC

On 30/12/2023 15:54, Anton Ertl wrote:
> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>> \ SORTI (slightly slower than SORTM, slightly faster than SORTI2)
>>>
>>> : sorti ( addr u -- )
>>> cells over + 0 -rot begin ( 0 al1 ah1 .. aln ahn )
>>> begin
>>> over cell+ over u>= while
>>> 2drop dup 0= if
>>> drop exit then
>>> repeat
>>> partition
>>> again ;
>>
>> Let's see if we can improve on this with the extended CASE (untested):
>>
>> : sorti3 ( addr u -- )
>> cells over + 0 -rot case ( 0 al1 ah1 .. aln ahn )
>> over cell+ over u< ?of partition contof
>> 2drop dup ?of contof
>> endcase
>> drop ;
>>
>> It looks neater and gets rid of the EXIT in the middle.
>
> But it suffers from me falling into Eaker's trap: ENDCASE already
> performs a DROP, so the DROP at the end is wrong:
>
> : sorti3 ( addr u -- )
> cells over + 0 -rot case ( 0 al1 ah1 .. aln ahn )
> over cell+ over u< ?of partition contof
> 2drop dup ?of contof
> endcase ;
>

Compare this with using the FOR-EACH construct discussed recently. THE
FOR-EACH definitions are:

: cs-drop-dest ( CS: dest -- ) postpone true postpone until ;

\ A workaround to get the MAP with locals working in GForth
[undefined] assume-live [if] : assume-live ; immediate [then]

: for-each
postpone ahead postpone assume-live postpone begin postpone 0<
postpone if postpone begin 3 cs-roll postpone then
; immediate

synonym do[ while [defined] [SwiftForth] [if] immediate [then]

: ]next
postpone repeat postpone then cs-drop-dest
; immediate

\ |> is the pipeline operator
: |> ( 0 -- ) \ or ( n -- n )
3 cs-pick postpone ?dup postpone 0= postpone until
; immediate

And SORTI3 using FOR-EACH could be:

: sorti3 ( ad u -- )
cells over + 0 -rot
for-each true
do[ over cell+ over u< dup >r if partition then r> |>
2drop dup 0= abs |>
]next drop
;

Untested on a real sort but tested with dummy data and dummy code for
the pipeline operations.

SORTi3 is written like that for comparison with your definition. I would
probably write it as:

: sort-partition \ There is probably a better name
over cell+ over u< dup >r if partition then r>
;

: ?done ( ad -- ad 0|1 ) dup 0= abs ;

: sorti3 ( ad u -- )
cells over + 0 -rot
for-each true
do[ sort-partition |> 2drop ?done |> ]next drop
;

The FOR-EACH TRUE is a bit awkward (i.e. no iterator needed) and
supports RUVIM's comment that the the iterator should just be first in
the pipeline. An alternative might be defining FOR[ that omits an
iterator and DO[. i.e. ... FOR[ ... |> ... etc ]NEXT

--
Gerry

Re: recursion is silly

<20240104101004.858@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25885&group=comp.lang.forth#25885

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 4 Jan 2024 18:45:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 115
Message-ID: <20240104101004.858@kylheku.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231228140434.720@kylheku.com> <nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
<87ttnu54qa.fsf@nightsong.com> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
Injection-Date: Thu, 4 Jan 2024 18:45:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e86d7ed55a4386262bd5785b65adb8f4";
logging-data="3952492"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1850JJ9qrL4uOzZfDIYiBs92d1tUne+0vc="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:lesecW9UKBnTKJqjNmHeHNjjcuw=
 by: Kaz Kylheku - Thu, 4 Jan 2024 18:45 UTC

On 2024-01-04, mhx <mhx@iae.nl> wrote:
>> Can the memoization "see" that once cc(N-5,5) is known,
>> cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
> [..]
>
> I never used naive memoization much because the table size becomes
> impractical for real-world problems. If it is known how to compress
> N-dim tables (in hopefully a simple way), I'd be very interested
> to know how it is done (even when N=1).

In some cases, eviction may be possible (i.e. caching).

If we consider memoized Fibonacci, once it has calculated fib(7),
it's not going to have to calculate fib(5) again. That value could
be evacuated from the table. In fact, I suspect that a LRU table
with only several entries would serve.

This is easily confirmed code-wise, using TXR Lisp:

$ txr -i fib.tl
Read-eval-disagree-downvote-insult treadmill initialized! Enter an expression:
1> (compile 'fib-rec)
#<vm fun: 2 param>
2> (pprof (fib 200))
malloc bytes: 74356
gc heap bytes: 66272
total: 140628
milliseconds: 2
453973694165307953197296969697410619233826
3> (trace fib-rec)
nil
4> (fib 8)
(fib-rec (8 #(nil nil nil))
(fib-rec (6 #(nil nil nil))
(fib-rec (4 #(nil nil nil))
(fib-rec (2 #(nil nil nil))
(fib-rec (0 #(nil nil nil))
1)
(fib-rec (1 #(nil nil nil))
1)
2)
(fib-rec (3 #((2 . 2) nil nil))
(fib-rec (1 #((2 . 2) nil nil))
1)
(fib-rec (2 #((2 . 2) nil nil))
2)
3)
5)
(fib-rec (5 #((4 . 5) (3 . 3) (2 . 2)))
(fib-rec (3 #((4 . 5) (3 . 3) (2 . 2)))
3)
(fib-rec (4 #((4 . 5) (3 . 3) (2 . 2)))
5)
8)
13)
(fib-rec (7 #((6 . 13) (5 . 8) (4 . 5)))
(fib-rec (5 #((6 . 13) (5 . 8) (4 . 5)))
8)
(fib-rec (6 #((6 . 13) (5 . 8) (4 . 5)))
13)
21)
34)
34

Calculating (fib 200) is instantaneous. The trace shows
that the caching with a three element table is sufficient;
E.g. once (fib-rec 5) is calculated, the second time
it is encountered, it no longer recurses to (fib-rec 3)
and (fib-rec 4); it just returns 8.

This is the code in fib.tl:

(defun fib-rec (n table)
(or (if (meq n 0 1) 1)
(cdr [find n table : car])
(flow (+ (fib-rec (ppred n) table)
(fib-rec (pred n) table))
(let f)
(cons n)
(set [(nrot table -1) 0])
(ret f))))

(defun fib (n)
(fib-rec n (vector 3)))

The auxiliary table parameter always holds the same 3 element
vector object throughout the recursion. The vector holds
cons pairs of the form (cons n (fib n)), thus mapping Fibonacci
indices to values.

Whenever there is a table miss, the nrot function is used to
rotate the table entries down, and the new value is placed
in the first element, [table 0].

In fact, a two element table is sufficient; if we change
the (vector 3) to (vector 2), the performance is not affected.

Moreover, it deosn't matter whether the recursive step is
(+ (fib-rec (ppred n) table) (fib-rec (pred n) table)) or
(+ (fib-rec (pred n) table) (fib-rec (ppred n) table));
a two-element cache is sufficient. (TXR Lisp is strictly
evaluated, left-to-right, like Common Lisp, so if we
vary the order of the arguments to +, we can control the
order in which the recursive calls take place.)

In conclusion, while memoized Fibonacci is great textbook
example for illustrating the power of memoization,
it woudl behoove the textbooks to also add the observation
that it still works with a two-slot LRU cache.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: recursion is silly

<87plyh53mf.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25886&group=comp.lang.forth#25886

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 04 Jan 2024 10:56:40 -0800
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <87plyh53mf.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231228140434.720@kylheku.com>
<nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
<87ttnu54qa.fsf@nightsong.com>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91a630b595e6e5bef44e319b69d624fa";
logging-data="3955253"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hgriCbbYteiKfusXLLxBY"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:wbRXCFC63Fzt4mD5dkk7h36pMUc=
sha1:Z5/bGz0RZYxuVd5zcsGisFpX9Ws=
 by: Paul Rubin - Thu, 4 Jan 2024 18:56 UTC

mhx@iae.nl (mhx) writes:
> If it is known how to compress N-dim tables (in hopefully a simple
> way), I'd be very interested to know how it is done (even when N=1).

In the Scheme code I posted, the "coordinate list" is used as a hash key.

Re: recursion is silly

<20240104114837.742@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25887&group=comp.lang.forth#25887

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 4 Jan 2024 19:49:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <20240104114837.742@kylheku.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231228140434.720@kylheku.com> <nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
<87ttnu54qa.fsf@nightsong.com> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
<20240104101004.858@kylheku.com>
Injection-Date: Thu, 4 Jan 2024 19:49:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e86d7ed55a4386262bd5785b65adb8f4";
logging-data="3970097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bxwYDywQUk4KHhqC3FFO1FSl2xMT1Xds="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Guqm3nrj80IouKpDMHtclvQES6U=
 by: Kaz Kylheku - Thu, 4 Jan 2024 19:49 UTC

On 2024-01-04, Kaz Kylheku <433-929-6894@kylheku.com> wrote:
> In conclusion, while memoized Fibonacci is great textbook
> example for illustrating the power of memoization,
> it woudl behoove the textbooks to also add the observation
> that it still works with a two-slot LRU cache.

However, due to the function activation frames, memory
use is still O(n).

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: recursion is silly

<87le946bga.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25888&group=comp.lang.forth#25888

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 04 Jan 2024 13:22:13 -0800
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <87le946bga.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231228140434.720@kylheku.com>
<nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
<87ttnu54qa.fsf@nightsong.com>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91a630b595e6e5bef44e319b69d624fa";
logging-data="3994129"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LYhG9h2bSEdNux9NgA86G"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:4QWlkpHnfNjABBtp0awVBNsvvfw=
sha1:qcHZY9gjEMpPGzICl2i3jPA7Pog=
 by: Paul Rubin - Thu, 4 Jan 2024 21:22 UTC

albert@cherry.(none) (albert) writes:
> Can the memoization "see" that once cc(N-5,5) is known,
> cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
> in the iterative version),

I'm not sure what you mean by that. The memoization scheme proceeds
recursively until it reaches a place where it knows how to get a
numerical answer. Then it computes and remembers that answer, so it can
use it the next time the calculation gets there.

> and that cc(N,5) becomes cc(N,4)+cc(N-5,5) . That is chill.

Yes that is right.

> (The iterative version also saves some 50% of the time going 50 -> 1)

I don't think I understand your iterative algorithm, but I think there
is one that actually doesn't have to remember the calculation results
all the way back to the beginning. It only has to remember the last 100
(or whatever the size of the largest coin is), and similarly for the
other coin sizes. I'll see if I can code that.

Re: recursion is silly

<806f415b-757f-4436-9443-e1477e1467efn@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25889&group=comp.lang.forth#25889

  copy link   Newsgroups: comp.lang.forth
X-Received: by 2002:a05:622a:8105:b0:429:7150:6813 with SMTP id jx5-20020a05622a810500b0042971506813mr60182qtb.10.1704407891064;
Thu, 04 Jan 2024 14:38:11 -0800 (PST)
X-Received: by 2002:a05:620a:a4f:b0:781:ef27:b994 with SMTP id
j15-20020a05620a0a4f00b00781ef27b994mr16921qka.2.1704407890449; Thu, 04 Jan
2024 14:38:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.forth
Date: Thu, 4 Jan 2024 14:38:10 -0800 (PST)
In-Reply-To: <87le946bga.fsf@nightsong.com>
Injection-Info: google-groups.googlegroups.com; posting-host=136.50.14.162; posting-account=AoizIQoAAADa7kQDpB0DAj2jwddxXUgl
NNTP-Posting-Host: 136.50.14.162
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <20231228140434.720@kylheku.com>
<nnd$6f8d4fb1$5509f70d@46490c8c7354685d> <87ttnu54qa.fsf@nightsong.com>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <87le946bga.fsf@nightsong.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <806f415b-757f-4436-9443-e1477e1467efn@googlegroups.com>
Subject: Re: recursion is silly
From: jim.brakefield@ieee.org (James Brakefield)
Injection-Date: Thu, 04 Jan 2024 22:38:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: James Brakefield - Thu, 4 Jan 2024 22:38 UTC

On Thursday, January 4, 2024 at 3:22:19 PM UTC-6, Paul Rubin wrote:
> albert@cherry.(none) (albert) writes:
> > Can the memoization "see" that once cc(N-5,5) is known,
> > cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
> > in the iterative version),
> I'm not sure what you mean by that. The memoization scheme proceeds
> recursively until it reaches a place where it knows how to get a
> numerical answer. Then it computes and remembers that answer, so it can
> use it the next time the calculation gets there.
> > and that cc(N,5) becomes cc(N,4)+cc(N-5,5) . That is chill.
> Yes that is right.
> > (The iterative version also saves some 50% of the time going 50 -> 1)
> I don't think I understand your iterative algorithm, but I think there
> is one that actually doesn't have to remember the calculation results
> all the way back to the beginning. It only has to remember the last 100
> (or whatever the size of the largest coin is), and similarly for the
> other coin sizes. I'll see if I can code that.

|>I think there is one that actually doesn't have to remember the calculation results
all the way back to the beginning.
To convert Fibonacci to a linear recursion one needs to keep two (usually the two previous) values
In Forth: swap, over, plus
On a register machine, one can do two iterations with two replace adds (a+b -> a, b+a -> b)
Also possible on a stack machine with the corresponding operators.

Re: recursion is silly

<87h6js5yeh.fsf@nightsong.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25891&group=comp.lang.forth#25891

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth
Subject: Re: recursion is silly
Date: Thu, 04 Jan 2024 18:04:06 -0800
Organization: A noiseless patient Spider
Lines: 5
Message-ID: <87h6js5yeh.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<20231228140434.720@kylheku.com>
<nnd$6f8d4fb1$5509f70d@46490c8c7354685d>
<87ttnu54qa.fsf@nightsong.com>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<87le946bga.fsf@nightsong.com>
<806f415b-757f-4436-9443-e1477e1467efn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="2c51adaaacf6164b89ca26cb99ccd848";
logging-data="4060480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VxcpG+LQtN02Cq2GMHKvp"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:EsvFdMJiVRhhl/UrC1TzvPGieds=
sha1:w8bz/msn3Oro+MsDNP+BaprCmPo=
 by: Paul Rubin - Fri, 5 Jan 2024 02:04 UTC

James Brakefield <jim.brakefield@ieee.org> writes:
> To convert Fibonacci to a linear recursion one needs to keep two
> (usually the two previous) values

This is about the coins problem, not Fibonacci.

Re: recursion is silly

<nnd$7d0e6362$6714e6de@046c865c6f32b5ed>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25893&group=comp.lang.forth#25893

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <87ttnu54qa.fsf@nightsong.com> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <87le946bga.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$7d0e6362$6714e6de@046c865c6f32b5ed>
Organization: KPN B.V.
Date: Fri, 05 Jan 2024 13:27:43 +0100
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe004.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 34
Injection-Date: Fri, 05 Jan 2024 13:27:43 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2267
 by: none - Fri, 5 Jan 2024 12:27 UTC

In article <87le946bga.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>albert@cherry.(none) (albert) writes:
>> Can the memoization "see" that once cc(N-5,5) is known,
>> cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
>> in the iterative version),
>
>I'm not sure what you mean by that. The memoization scheme proceeds
>recursively until it reaches a place where it knows how to get a
>numerical answer. Then it computes and remembers that answer, so it can
>use it the next time the calculation gets there.
>
>> and that cc(N,5) becomes cc(N,4)+cc(N-5,5) . That is chill.
>
>Yes that is right.
>
>> (The iterative version also saves some 50% of the time going 50 -> 1)
>
>I don't think I understand your iterative algorithm, but I think there
>is one that actually doesn't have to remember the calculation results
>all the way back to the beginning. It only has to remember the last 100
>(or whatever the size of the largest coin is), and similarly for the
>other coin sizes. I'll see if I can code that.

I thought of that too, and it doesn't work. Something inherent of
two way recursion.

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$7561b49c$43c6a88e@a8960ed47cc85118>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25894&group=comp.lang.forth#25894

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <62ce04af2582b47ac60b4a504694156f@news.novabbs.com> <20240104101004.858@kylheku.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$7561b49c$43c6a88e@a8960ed47cc85118>
Organization: KPN B.V.
Date: Fri, 05 Jan 2024 13:37:25 +0100
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 133
Injection-Date: Fri, 05 Jan 2024 13:37:25 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 5219
 by: none - Fri, 5 Jan 2024 12:37 UTC

In article <20240104101004.858@kylheku.com>,
Kaz Kylheku <433-929-6894@kylheku.com> wrote:
>On 2024-01-04, mhx <mhx@iae.nl> wrote:
>>> Can the memoization "see" that once cc(N-5,5) is known,
>>> cc(N-4,5) = cc(N-5,5), up till (cc(N-1,5) (which are zero till now
>> [..]
>>
>> I never used naive memoization much because the table size becomes
>> impractical for real-world problems. If it is known how to compress
>> N-dim tables (in hopefully a simple way), I'd be very interested
>> to know how it is done (even when N=1).
>
>In some cases, eviction may be possible (i.e. caching).
>
>If we consider memoized Fibonacci, once it has calculated fib(7),
>it's not going to have to calculate fib(5) again. That value could
>be evacuated from the table. In fact, I suspect that a LRU table
>with only several entries would serve.
>
>This is easily confirmed code-wise, using TXR Lisp:
>
>$ txr -i fib.tl
>Read-eval-disagree-downvote-insult treadmill initialized! Enter an expression:
>1> (compile 'fib-rec)
>#<vm fun: 2 param>
>2> (pprof (fib 200))
>malloc bytes: 74356
>gc heap bytes: 66272
>total: 140628
>milliseconds: 2
>453973694165307953197296969697410619233826
>3> (trace fib-rec)
>nil
>4> (fib 8)
>(fib-rec (8 #(nil nil nil))
> (fib-rec (6 #(nil nil nil))
> (fib-rec (4 #(nil nil nil))
> (fib-rec (2 #(nil nil nil))
> (fib-rec (0 #(nil nil nil))
> 1)
> (fib-rec (1 #(nil nil nil))
> 1)
> 2)
> (fib-rec (3 #((2 . 2) nil nil))
> (fib-rec (1 #((2 . 2) nil nil))
> 1)
> (fib-rec (2 #((2 . 2) nil nil))
> 2)
> 3)
> 5)
> (fib-rec (5 #((4 . 5) (3 . 3) (2 . 2)))
> (fib-rec (3 #((4 . 5) (3 . 3) (2 . 2)))
> 3)
> (fib-rec (4 #((4 . 5) (3 . 3) (2 . 2)))
> 5)
> 8)
> 13)
> (fib-rec (7 #((6 . 13) (5 . 8) (4 . 5)))
> (fib-rec (5 #((6 . 13) (5 . 8) (4 . 5)))
> 8)
> (fib-rec (6 #((6 . 13) (5 . 8) (4 . 5)))
> 13)
> 21)
> 34)
>34
>
>Calculating (fib 200) is instantaneous. The trace shows
>that the caching with a three element table is sufficient;
>E.g. once (fib-rec 5) is calculated, the second time
>it is encountered, it no longer recurses to (fib-rec 3)
>and (fib-rec 4); it just returns 8.
>
>This is the code in fib.tl:
>
>(defun fib-rec (n table)
> (or (if (meq n 0 1) 1)
> (cdr [find n table : car])
> (flow (+ (fib-rec (ppred n) table)
> (fib-rec (pred n) table))
> (let f)
> (cons n)
> (set [(nrot table -1) 0])
> (ret f))))
>
>(defun fib (n)
> (fib-rec n (vector 3)))
>
>The auxiliary table parameter always holds the same 3 element
>vector object throughout the recursion. The vector holds
>cons pairs of the form (cons n (fib n)), thus mapping Fibonacci
>indices to values.
>
>Whenever there is a table miss, the nrot function is used to
>rotate the table entries down, and the new value is placed
>in the first element, [table 0].
>
>In fact, a two element table is sufficient; if we change
>the (vector 3) to (vector 2), the performance is not affected.
>
>Moreover, it deosn't matter whether the recursive step is
>(+ (fib-rec (ppred n) table) (fib-rec (pred n) table)) or
>(+ (fib-rec (pred n) table) (fib-rec (ppred n) table));
>a two-element cache is sufficient. (TXR Lisp is strictly
>evaluated, left-to-right, like Common Lisp, so if we
>vary the order of the arguments to +, we can control the
>order in which the recursive calls take place.)
>
>In conclusion, while memoized Fibonacci is great textbook
>example for illustrating the power of memoization,
>it woudl behoove the textbooks to also add the observation
>that it still works with a two-slot LRU cache.
+1

You have managed to arrive at this iterative version, that keeps
track of only two values.
: FIB
>R 0 1
R> 0 ?DO SWAP OVER + 1 +LOOP
DROP
;

(Interested to see this coded in lisp. )

>
>Mastodon: @Kazinator@mstdn.ca

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$76aec35d$18a0a930@a8960ed47cc85118>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25895&group=comp.lang.forth#25895

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <62ce04af2582b47ac60b4a504694156f@news.novabbs.com> <20240104101004.858@kylheku.com> <20240104114837.742@kylheku.com>
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$76aec35d$18a0a930@a8960ed47cc85118>
Organization: KPN B.V.
Date: Fri, 05 Jan 2024 13:42:58 +0100
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe006.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 28
Injection-Date: Fri, 05 Jan 2024 13:42:58 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1937
 by: none - Fri, 5 Jan 2024 12:42 UTC

In article <20240104114837.742@kylheku.com>,
Kaz Kylheku <433-929-6894@kylheku.com> wrote:
>On 2024-01-04, Kaz Kylheku <433-929-6894@kylheku.com> wrote:
>> In conclusion, while memoized Fibonacci is great textbook
>> example for illustrating the power of memoization,
>> it woudl behoove the textbooks to also add the observation
>> that it still works with a two-slot LRU cache.
>
>However, due to the function activation frames, memory
>use is still O(n).

In the coins example I did not use memoization per se, but
rather a reverse memoization. Please check that the memory
usage with the Forth example for FIB, is O(1), also with
reverse memoization.
[I had success with this technique in several of the 400 problems
of projecteuler.net that I solved.]
>
>--
>Mastodon: @Kazinator@mstdn.ca

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<20240105104414.216@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25896&group=comp.lang.forth#25896

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Fri, 5 Jan 2024 18:53:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <20240105104414.216@kylheku.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
<20240104101004.858@kylheku.com> <nnd$7561b49c$43c6a88e@a8960ed47cc85118>
Injection-Date: Fri, 5 Jan 2024 18:53:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="624dae6c469856f06c49a782a2b1407e";
logging-data="275916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZvuVAtxaPbibRQKumUKVd6FINacKz0QI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:2O+PX4Vri9J2/HkDQc5cu8UkL+0=
 by: Kaz Kylheku - Fri, 5 Jan 2024 18:53 UTC

On 2024-01-05, albert@cherry.(none) (albert) <albert@cherry> wrote:
> You have managed to arrive at this iterative version, that keeps
> track of only two values.

Well, yes; that is understood and where the intution comes from for why
Memoization implicitly reduces the exponential explosion in Fibonacci.
It must be effectively iterating on the recurrence in the
straightforward way, since it takes linear time.

The recurrence relation implies a two-element state; I guessed at
a cache size of three items as a fudge factor; when that
was debugged, I reduced to two.

Sometimes, the Fibonacci recurrence is expressed as a matrix
multiplication on a state vector [a b]

[1 1][a] => [a + b]
[1 0][b] [ a ]

a receives the sum of a + b, while b receives the prior value of a.

Raising this matrix to an arbitrary integer power is how we arrive
at the closed form solution.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: recursion is silly

<44ad19ee75efa3982019aea1a6fc0b8f@news.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25897&group=comp.lang.forth#25897

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: mhx@iae.nl (mhx)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Fri, 5 Jan 2024 19:44:27 +0000
Organization: novaBBS
Message-ID: <44ad19ee75efa3982019aea1a6fc0b8f@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <62ce04af2582b47ac60b4a504694156f@news.novabbs.com> <20240104101004.858@kylheku.com> <nnd$7561b49c$43c6a88e@a8960ed47cc85118> <20240105104414.216@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="2400834"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$ufb2CsWwcEaZhjok2hOXvuw5kZEuLtoLQHf3i0nyHWW5yQAixhSX2
X-Rslight-Posting-User: 463cbf1a76c808942982a163321348c75477c065
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: mhx - Fri, 5 Jan 2024 19:44 UTC

Kaz Kylheku wrote:

> Sometimes, the Fibonacci recurrence is expressed as a matrix
> multiplication on a state vector [a b]

> [1 1][a] => [a + b]
> [1 0][b] [ a ]

> a receives the sum of a + b, while b receives the prior value of a.

> Raising this matrix to an arbitrary integer power is how we arrive
> at the closed form solution.

Really nice! I'll keep this general idea in a safe place.

-marcel

Re: recursion is silly

<20240105131349.9@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25898&group=comp.lang.forth#25898

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Fri, 5 Jan 2024 21:24:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <20240105131349.9@kylheku.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<nnd$4b6f18cf$1a843abd@36af38f06eabba0f>
<62ce04af2582b47ac60b4a504694156f@news.novabbs.com>
<20240104101004.858@kylheku.com> <nnd$7561b49c$43c6a88e@a8960ed47cc85118>
<20240105104414.216@kylheku.com>
<44ad19ee75efa3982019aea1a6fc0b8f@news.novabbs.com>
Injection-Date: Fri, 5 Jan 2024 21:24:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="624dae6c469856f06c49a782a2b1407e";
logging-data="321273"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mqe0t9dHDUp/8yTYKozs9LEmz5XgfG3w="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:H1fFuVhzyXPBR9nDs4ncba81np0=
 by: Kaz Kylheku - Fri, 5 Jan 2024 21:24 UTC

On 2024-01-05, mhx <mhx@iae.nl> wrote:
> Kaz Kylheku wrote:
>
>> Sometimes, the Fibonacci recurrence is expressed as a matrix
>> multiplication on a state vector [a b]
>
>> [1 1][a] => [a + b]
>> [1 0][b] [ a ]
>
>> a receives the sum of a + b, while b receives the prior value of a.
>
>> Raising this matrix to an arbitrary integer power is how we arrive
>> at the closed form solution.
>
> Really nice! I'll keep this general idea in a safe place.

You don't have to; it's found in textbooks.

Using linear algebra techniques found in introductory texts, you can do
this this:

n
[1 1]
[1 0]

The details escape me, much like a departing youthful appearance, but
basically we can factor it into a form

n
U X V

where U, X, V are all 2x2 matrices. The middle one X is special
in that it is diagonal (consisting of a diagonal trace of eigenvalues).
A diagonal matrix is easy to exponentiate.

Something like that.

That gives us that closed form formula for Fibonacci with those
square roots of five in it.

Closely relating to the Golden Ratio! Which is (/ (+ 1 (sqrt 5)) 2).
The ratio of successive values of the Fibonacci numbers approaches
the golden ratio.

1> (/ (fib 50) (fib 49))
1.61803398874989

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: recursion is silly

<0d86e120a26d7c6a01ce20cb1847d3b2@news.novabbs.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25900&group=comp.lang.forth#25900

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: mhx@iae.nl (mhx)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Sat, 6 Jan 2024 09:15:12 +0000
Organization: novaBBS
Message-ID: <0d86e120a26d7c6a01ce20cb1847d3b2@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <62ce04af2582b47ac60b4a504694156f@news.novabbs.com> <20240104101004.858@kylheku.com> <nnd$7561b49c$43c6a88e@a8960ed47cc85118> <20240105104414.216@kylheku.com> <44ad19ee75efa3982019aea1a6fc0b8f@news.novabbs.com> <20240105131349.9@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="2463628"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$OgSxVSRVk18pePRNkiBbdOXAP9LDlSjymnu1FrWNBLSeCAwbX0AQy
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 463cbf1a76c808942982a163321348c75477c065
 by: mhx - Sat, 6 Jan 2024 09:15 UTC

Kaz Kylheku wrote:

> On 2024-01-05, mhx <mhx@iae.nl> wrote:
>> Kaz Kylheku wrote:
>>
>>> Sometimes, the Fibonacci recurrence is expressed as a matrix
>>> multiplication on a state vector [a b]
>>
>>> [1 1][a] => [a + b]
>>> [1 0][b] [ a ]
>>
>>> a receives the sum of a + b, while b receives the prior value of a.
>>
>>> Raising this matrix to an arbitrary integer power is how we arrive
>>> at the closed form solution.
>>
>> Really nice! I'll keep this general idea in a safe place.

> You don't have to; it's found in textbooks.

It strongly connects to the control engineering approaches I use for
analyzing recurrences in switched electronic systems.

> Using linear algebra techniques found in introductory texts, you can do
> this this:

> n
> [1 1]
> [1 0]

> The details escape me, much like a departing youthful appearance, but
> basically we can factor it into a form

> n
> U X V

> where U, X, V are all 2x2 matrices. The middle one X is special
> in that it is diagonal (consisting of a diagonal trace of eigenvalues).
> A diagonal matrix is easy to exponentiate.

Tricky one to write from scratch :--) I implemented the Pade approximation
(the stablest one) for it when my appearance was still youthful.

> Something like that.

> That gives us that closed form formula for Fibonacci with those
> square roots of five in it.

> Closely relating to the Golden Ratio! Which is (/ (+ 1 (sqrt 5)) 2).
> The ratio of successive values of the Fibonacci numbers approaches
> the golden ratio.

Almost like a party trick. I love to find elegant techniques in
unrelated fields of research that solve (my) practical problems in
unexpected ways.

> 1> (/ (fib 50) (fib 49))
> 1.61803398874989

-marcel

Re: recursion is silly

<2024Jan6.175116@mips.complang.tuwien.ac.at>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=25911&group=comp.lang.forth#25911

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Sat, 06 Jan 2024 16:51:16 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 40
Message-ID: <2024Jan6.175116@mips.complang.tuwien.ac.at>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <nnd$4b6f18cf$1a843abd@36af38f06eabba0f> <62ce04af2582b47ac60b4a504694156f@news.novabbs.com> <20240104101004.858@kylheku.com> <nnd$7561b49c$43c6a88e@a8960ed47cc85118> <20240105104414.216@kylheku.com> <44ad19ee75efa3982019aea1a6fc0b8f@news.novabbs.com> <20240105131349.9@kylheku.com> <0d86e120a26d7c6a01ce20cb1847d3b2@news.novabbs.com>
Injection-Info: dont-email.me; posting-host="444defe7ba9ec3dff1c2e1e9c29c85d7";
logging-data="733805"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jqPykm0535vD4y09eJznG"
Cancel-Lock: sha1:DWLjTtg0Wg4jqPr44he/7ZpIM48=
X-newsreader: xrn 10.11
 by: Anton Ertl - Sat, 6 Jan 2024 16:51 UTC

mhx@iae.nl (mhx) writes:
>Kaz Kylheku wrote:
>> Closely relating to the Golden Ratio! Which is (/ (+ 1 (sqrt 5)) 2).
>> The ratio of successive values of the Fibonacci numbers approaches
>> the golden ratio.
>
>Almost like a party trick. I love to find elegant techniques in
>unrelated fields of research that solve (my) practical problems in
>unexpected ways.

Not sure if you wrote this about the golden ratio, but given the way
the golden ratio phi is defined, I find it unsurprising that

phi = lim(n->inf) fib(n+1)/fib(n)

The definition of the golden ratio is

(a+b)/a = a/b = phi (with a>b>0)

or, as described in <https://en.wikipedia.org/wiki/Golden_ratio>:

| a+b is to a as a is to b

Given that's exactly how Fibonacci numbers are formed, if the sequence
of ratios fib(n+1)/fib(n) converges, the result must be the golden
ratio.

It's equally unsurprising that for the Lucas numbers, which are formed
in the same way, but with different base values (2 and 1 instead of 0
and 1), the limit of L(n+1)/L(n) is also the golden ratio.

Conversely, you can compute the nth Fibonacci number using a closed
formula that involves phi.

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2023: https://euro.theforth.net/2023

Pages:123
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor