From - Mon Oct 16 17:32:03 2000 Path: news!ringer.cs.utsa.edu!nntp.twtelecom.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!howland.erols.net!torn!sodalite.nbnet.nb.ca!newsfeed.unb.ca!Gateway.UNB.ca From: Roger Hui Newsgroups: comp.lang.apl Subject: Re: Please help: Maximum Arithmetic Progression Date: 14 Oct 2000 06:51:43 -0300 Organization: UofNB News Gateway, Fredericton, CANADA Lines: 33 Message-ID: <20001014.065132.12153@sol.sun.csd.unb.ca> Reply-To: Roger Hui NNTP-Posting-Host: sol.sun.csd.unb.ca X-Trace: malakbel.unb.ca 971517110 26292 131.202.3.6 (14 Oct 2000 09:51:50 GMT) X-Complaints-To: usenet@malakbel.unb.ca NNTP-Posting-Date: 14 Oct 2000 09:51:50 GMT Xref: news comp.lang.apl:6406 > I am trying to develop some maths software and for a while > I tried to write an APL function to get the maximum arithmetic > progression from a set of integers. The code I tried is too horrible > to post here, so are there any smart one liners that will do this ? > > Tony Goddard > http://www.d4maths.co.uk f=: (2&(=/\)) @ (2&(-/\)) g=: * #~ @ (#;.2) @ (,&1) @ (2&(~:/\)) h=: (i. >./) @ g @ f x=: 2 1 4 7 9 6 18 16 14 12 15 18 3 7 x , (f x) ,: (g f x) 2 1 4 7 9 6 18 16 14 12 15 18 3 7 0 1 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 2 2 0 1 0 0 0 0 h x 6 f computes the boolean vector marking where a first difference (FD) is equal to the next FD; g computes the lengths of subsequences of 1s; h applies (i. >./) to find the index of the maximal element, which is the beginning index of the maximum arithmetic progression. If you want the solution all on one line, here it is: h f. (i. >./)@(* #~@(#;.2)@(,&1)@(2&(~:/\)))@(2&(=/\)@(2&(-/\)))