Building the result bit by bit:
list3 =: |:@:(;@:((,.~ i.&.<:)&.>))@:(i.&.<:)
list3 5
1 1 2 1 2 3
2 3 3 4 4 4
(different order from your verbs)
more J-like is to create the big array and throw out what you don't
want:
list4 =: (#"1~ </)@:|:@:(}. (#: i.@(*/))@(2&$))
list4 5
1 1 1 2 2 3
2 3 4 3 4 4
Henry Rich
Ian Gorse wrote:
> Hi list,
>
> I have been trying to find out a way how I can list all the positive
> numbers where a < b < c.
>
> For example, when c = 5, the output would be something like
> 2 6 $ 1 1 1 2 2 3 2 3 4 3 4 4 NB. example
> 1 1 1 2 2 3
> 2 3 4 3 4 4
> where the first row is a list of 'a' and the second row is a list of 'b'
>
> I have managed to do it with an explicit loop, but am looking for an
> alternative solution
>
> NB. Bruteforce
> list=: monad : 0
> a=.1
> b=.2
> c=.y
> result=.''
> while. b < c
> do.
> if. a < b do. result =. result , a , b end.
> b=.>:b
> if. b=c do. a=. >:a [ b=.a+1 end.
> end.
> n=. 2 ,~ 2 %~ #result
> |: n $ result
> )
> list 5
> 1 1 1 2 2 3
> 2 3 4 3 4 4
>
> After starting this email, I noticed a pattern when I was running the
> script with higher values such as c= 10,
> and managed to re-write the script, instead of looping through all
> possible values (which is what my fist attempt does) I now use some
> simple math to find all the values instead.
>
> list2 =: monad : 0
> a=.1
> b=.2
> c=.y
> al=.'' NB. holds a list of a's
> bl=.'' NB. holds a list of b's
> while. a < c
> do.
> b=.(c-1)-a
> al=. al, b $ a
> bl=. bl , a+ >:i.b
> a=.>:a
> end.
> n=. 2, (#al)
> n $ al,bl
> )
> list2 5
> 1 1 1 2 2 3
> 2 3 4 3 4 4
>
> ts 'list 1000'
> 3.24764 1.25855e7
> ts 'list2 1000'
> 0.046057 1.25852e7
>
> As you can see, list2 is by far a lot quicker than my original idea,
> but it still requires an explicit loop.
> Is there a solution to find a < b < c without such explicit loop?
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm