This is a kind of reversed dynamic programming.
Recursion is silly (if you can avoid it).
I recently draw attention to a toy lisp in Forth.
The example program was a scheme program that counts
how many changes in coins are possible for e.g. 100 dollarcent.
The lisp had to improved to be able to compile the program.
The improved version can hardly manage an input of 100 and takes
a noticable time, 200 and following fails.
What do you mean iterative?
An iterative function is one that loops to
repeat some part of the code, and a recursive
function is one that calls itself again to
repeat the code
What do you mean iterative? For instance, Gerald Sussman and Harold
Abelson classify as `iterative' the procedure
(def (f n [acc 1])
(if (= n 1)
1
(f (dec n) (* acc n)))),
which is self-referential.
1000000 | 1333983445341383545001 | 15.24 | 580696
I guess you are computing something different?
The time for 1,000,000 is also quite large, 15.24s vs 10ms (with iForth).
Julieta Shem <jshem@yaxenu.org> writes:
What do you mean iterative? For instance, Gerald Sussman and Harold
Abelson classify as `iterative' the procedure
(def (f n [acc 1])
(if (= n 1)
1
(f (dec n) (* acc n)))),
which is self-referential.
Are you sure? IIRC they differentiate between programs and processes.
So your function would be a recursive program but it doesn't create a recursive process (assumed the implementation is tail call optimized).
albert@cherry.(none) (albert) writes:
You must be kidding. The memoization using standard tools impressed me.
Compared to the result below it is only one order of magnitude.
I see, so the algorithms are basically similar. I haven't examined your
code closely yet. But I see you are using a much more efficient memo
table than I am. Yours is indexed on the offset into the coin list,
while mine uses the entire list. I might try adapting mine, maybe to
use arrays instead of a hash table.
I noticed I got an 8x speedup and 5x memory reduction by simply
reversing the order of the coin list, i.e. using (100 50 25 10 5 1)
instead of (1 5 10 25 50 100).
In reality, 50 cent coins and dollar coins are rarely used in the US.
50 cent coins are large enough to be cumbersome, and dollar coins never >became popular because they are too easily confused with quarters (they
are just slightly larger).
Canada has all the coins that the US has, plus a 2 dollar coin, and they >actually use them. France before the Euro had even more, I believe.
Somebody who doesn’t like recursion, I wonder what they’re likely to think
of continuations ...
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Somebody who doesn’t like recursion, I wonder what they’re likely to
think of continuations ...
They could like continuations because they might like goto statements.
:-)
They could like continuations because they might like goto statements. :-)
On Fri, 29 Dec 2023 00:14:01 -0300, Julieta Shem wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Somebody who doesn’t like recursion, I wonder what they’re likely to >>> think of continuations ...
They could like continuations because they might like goto statements.
:-)
Interestingly, gotos are the one thing that cannot (easily, in general) be expressed as continuations ...
``[...] Like [goto statements], continuations can be abused in all sorts
of bizarre manners; virtually every argument that [Dijkstra] makes in
[Goto Considered Harmful] can be applied to continuations.''
On Fri, 29 Dec 2023 13:35:47 -0300, Julieta Shem wrote:
``[...] Like [goto statements], continuations can be abused in all sorts
of bizarre manners; virtually every argument that [Dijkstra] makes in
[Goto Considered Harmful] can be applied to continuations.''
But consider the following list:
Loop constructs:
-- useful/desirable? Yes.
-- can be practicably implemented with continuations? Yes.
Exceptions:
-- useful/desirable? Yes.
-- can be practicably implemented with continuations? Yes.
Coroutines:
-- useful/desirable? Yes.
-- can be practicably implemented with continuations? Yes.
Arbitrary gotos:
-- useful/desirable? Not so.
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Arbitrary gotos:
-- useful/desirable? Not so.
Very useful, very desirable.
On Fri, 29 Dec 2023 23:22:53 -0300, Julieta Shem wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Arbitrary gotos:
-- useful/desirable? Not so.
Very useful, very desirable.
Come on ... you don’t mean you code that way, do you?
\ SORTM (best-performing)
: sort2 ( al ah -- )
begin
over cell+ over u< while
partition recurse
repeat
2drop ;
- anton--
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.
And yes, most of the time (or always?) there
does exist iterative counterpart solution
What do you mean iterative? For instance, Gerald Sussman and Harold
Abelson classify as `iterative' the procedure
(def (f n [acc 1])
(if (= n 1)
1
(f (dec n) (* acc n)))),
which is self-referential.
The Python equivalent (untested) is:
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
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).
The ratio of successive values of the Fibonacci numbers approaches the
golden ratio.
Puzzle:
Could we use this information somehow to neatly build an amplifier whose closed-loop gain is the Golden Ratio, using only resistors that have integer-valued resistances in Ohms?
I might post this to the Electronics Stack Exchange.
On Fri, 5 Jan 2024 21:24:00 -0000 (UTC), Kaz Kylheku wrote:
The ratio of successive values of the Fibonacci numbers approaches the
golden ratio.
The golden ratio is also obtained by just about the simplest of all
continued fractions:
1
1 + -------------
1
1 + ---------
1
1 + -----
...
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 405 |
Nodes: | 16 (2 / 14) |
Uptime: | 52:52:06 |
Calls: | 8,498 |
Calls today: | 11 |
Files: | 13,203 |
Messages: | 5,916,947 |
Posted today: | 1 |