Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.etla.org!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: revisiting a prgramming problem Date: Wed, 22 Oct 2014 02:13:40 -0400 Organization: Aioe.org NNTP Server Lines: 42 Message-ID: References: <2ccb76c4-b929-43fe-b53e-9fd85ace01a6@googlegroups.com> <43dda9f8-27b0-4014-aa4a-9d534193ba1a@googlegroups.com> <5446822d$0$2885$e4fe514c@news2.news.xs4all.nl> <5f15be60-2307-4880-90ba-f7d969afc2c2@googlegroups.com> NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:34082 On Tue, 21 Oct 2014 13:17:45 -0400, Mark Wills wrote: > On Tuesday, 21 October 2014 16:57:19 UTC+1, The Beez wrote: > After you have this first program written, and everybody is cheering > for you, write another program that takes *2* numbers and squares > each of them, adding the products together. Then you are done! >> >> Duh.. : sumofsquare over max dup * >r max dup * r> + ; >> > That's not how I interpret Hugh's challenge. It says take two numbers, > not the highest two of three. Perhaps I'm reading it wrong... > > : sos ( a b -- n ) dup * >r dup * r> + ; Hugh broke the problem into two parts: 1) maximum two of three 2) sum of squares of results of #1 That was his brain teaser. #1 really needs to be 2 parts. AISI, it's really a three part problem because the three values really need to be sorted first, largest to smallest, to _safely_ remove or drop the smallest, thereby leaving the two largest. : 2SORT 2DUP < IF SWAP THEN ; : 2MAX 2SORT ROT 2SORT DROP ; : SQSUM 2MAX DUP * SWAP DUP * + ; Expanded, it's only a hair longer longer than the one of Beez' which I think works correctly. It doesn't need the return stack. And, you might be able to work MAX in for one of the 2SORTS that way, shortening it to the same length or thereabouts. Rod Pemberton