Oo, or even

: triples ( n -- seq )
    iota rest 3 [ first3 triple? ] filter-combinations ;


On Mon, May 20, 2013 at 7:06 PM, Alex Vondrak <ajvond...@gmail.com> wrote:

> Or, going by the algorithms in the wiki:
>
> ```
> USING: arrays kernel locals math math.ranges sequences
> sequences.extras ;
>
> :: triples ( n -- seq )
>     n [1,b] [| x |
>         x n [a,b] [| y |
>             y n [a,b]
>             [| z | x sq y sq + z sq = ]
>             [| z | x y z 3array ]
>             filter-map
>         ] map concat
>     ] map concat ;
> ```
>
> Fun bug report: using `map-concat` from `sequences.extras` fails to
> compile for me---complains about using `call` on a run-time computed value.
>
> But, I really like your approach using `all-combinations`.  It yields a
> much nicer-looking version:
>
> ```
> : triple? ( x y z -- ? )
>     [ sq ] tri@ [ + ] dip = ;
>
> : triples ( n -- seq )
>     iota rest 3 all-combinations [ first3 triple? ] filter ;
> ```
>
> Regards,
> --Alex Vondrak
>
>
>
> On Mon, May 20, 2013 at 8:18 AM, _ _ <wzr...@gmail.com> wrote:
>
>> ! Copyright (C) 2013 Your name.
>> ! See http://factorcode.org/license.txt for BSD license.
>> USING: kernel sequences math math.functions arrays math.combinatorics ;
>> IN: pythagorean-triples
>>
>> : a-b-list ( n -- seq )
>>     iota rest 2 all-combinations ;
>>
>> : a-b-c ( seq1 -- seq2 )
>>     [ first ]
>>     [ second ]
>>     [ [ first sq ] [ second sq ] bi + sqrt ]
>>     tri
>>     3array ;
>>
>> : a-b-c-list ( n -- seq )
>>     a-b-list [ a-b-c ] map ;
>>
>> : float.0? ( n -- ? )
>>     dup floor 0 ~ ;
>>
>> : (pythagoreans) ( n -- seq )
>>     a-b-c-list [ third float.0? ] filter ;
>>
>> : pythagoreans ( n -- seq )
>>     (pythagoreans) [ [ first ] [ second ] [ third >integer ] tri 3array ]
>> map ;
>>
>>
>> ------------------------------------------------------------------------------
>> AlienVault Unified Security Management (USM) platform delivers complete
>> security visibility with the essential security capabilities. Easily and
>> efficiently configure, manage, and operate all of your security controls
>> from a single console and one unified framework. Download a free trial.
>> http://p.sf.net/sfu/alienvault_d2d
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>
>
------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to