Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #53560
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Challenge for Christmas |
| Date | 2016-12-27 13:12 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2016Dec27.141206@mips.complang.tuwien.ac.at> (permalink) |
| References | (4 earlier) <1795abbc-3937-419e-9699-4be39eff5aef@googlegroups.com> <17d49d3a-7ee2-462f-9206-94c8953ad989@googlegroups.com> <699c9f4f-1370-481e-a8f4-cc7ab5ba14a7@googlegroups.com> <81704e95-91eb-446c-ae1f-b826bc880941@googlegroups.com> <2016Dec26.165320@mips.complang.tuwien.ac.at> |
anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>lehs <skydda.barnen@gmail.com> writes:
>>For the buffer 6,8,12 it doesn't work for 576=3D6*8*12, but I don't know if=
>> there is a main error or a small error.
>
>216=6*6*6=8*3*3*3 is also a problematic case. No simple way to deal
>with such cases comes to my mind. The simplest more complex ways is
>probably a search with backtracking or somesuch.
I started with my first attempt, added a backtracking search, and
arrived at this:
: product? recursive { n addr u -- f }
\ is n a product of the numbers in addr u?
n 1 = if
true exit then
addr u cells + addr ?do
n i @ /mod { n1 } 0= if
n1 addr u product? if
true unloop exit then
then
cell +loop
false ;
Albert van der Horst's program
<o3rg8d$7q0$1@cherry.spenarnc.xs4all.nl> uses the same approach; a
difference from his program is that I used locals. A particularly
nice use of locals is the "{ n1 }" part: Without that I had to use a
SWAP in that place (not so bad), and also have an ELSE branch in the
outer IF to DROP this number (pretty bad).
This version has three IFs, and a triple-nested control structure. I
wanted to see if I could use THROW and CATCH to simplify the control
flow in this recursive search: On successfully reaching the target
(1), the following program THROWs rather than returning a flag that
has to be handled again and again:
: product1 recursive { n addr u -- }
\ throws TRUE if n is a product of the numbers in addr u
\ otherwise exits without throwing
n 1 = throw
addr u cells + addr ?do
n i @ /mod { n1 } 0= if
n1 addr u product1 then
cell +loop ;
: product1? ( n addr u -- f )
\ is n a product of the numbers in addr u?
['] product1 catch dup if nip nip nip then ;
The other case is when the search space is exhausted; in that case
PRODUCT1 just exits, and CATCH returns 0, exactly what we want in that
case. However, in the throwing case, CATCH restores the stack depth,
so we have to eliminate the additional stack items in this case.
Is the THROWing version better? What's your opinion?
- 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: http://www.forth200x.org/forth200x.html
EuroForth 2016: http://www.euroforth.org/ef16/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-24 06:10 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-24 14:17 -0800
Re: Challenge for Christmas mhx@iae.nl - 2016-12-24 15:37 -0800
Re: Challenge for Christmas mhx@iae.nl - 2016-12-24 15:43 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-25 01:25 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-25 06:59 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-25 07:26 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-25 07:57 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-25 08:07 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-25 08:25 -0800
Challenge for Christmas Ilya Tarasov <ilya74.tarasov@gmail.com> - 2016-12-25 07:39 -0800
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-25 16:52 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-25 09:33 -0800
Re: Challenge for Christmas Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-12-25 18:15 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-25 13:21 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-25 16:51 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 00:48 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 01:36 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 01:39 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-26 04:36 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 07:40 -0800
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-26 15:53 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 08:57 -0800
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-27 13:12 +0000
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-27 16:58 +0100
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-27 16:01 +0000
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-27 19:10 +0100
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-27 18:08 +0000
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-27 20:13 +0100
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-28 08:19 +0000
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-28 12:49 +0100
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-28 14:26 +0000
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-29 13:13 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-27 12:29 -0800
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-27 22:25 +0100
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-27 14:19 -0800
Re: Challenge for Christmas november.nihal@gmail.com - 2016-12-27 14:14 -0800
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-28 11:06 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-28 04:22 -0800
Re: Challenge for Christmas john <anon@example.com> - 2016-12-26 13:05 +0000
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 07:45 -0800
Re: Challenge for Christmas Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-12-26 07:44 +0000
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-26 13:21 +0000
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-26 13:57 +0000
Re: Challenge for Christmas anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-26 14:58 +0000
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-26 17:24 +0100
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 09:15 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-26 11:45 -0800
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-30 10:47 -0800
Re: Challenge for Christmas albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-12-31 15:31 +0100
Re: Challenge for Christmas lehs <skydda.barnen@gmail.com> - 2016-12-31 10:53 -0800
csiph-web