[Jprogramming] detecting cycles
Henry Rich
HenryHRich at nc.rr.com
Mon Dec 11 03:15:54 HKT 2006
If you do want to explicitly track intermediates, this
seems to work:
checkpower =: 2 : 'u^:(>:i.>:n)@{:^:(0=(i: {.))^:_&.,:'
(+ {&_1 _1 5@*) checkpower 6 ]100
3
The inner verb operates on things that are lists of verb
results; the loop terminates when the first element of the
list is found somewhere other than the first position.
The initial input is turned into a list to get started.
Henry Rich
> -----Original Message-----
> From: programming-bounces at jsoftware.com
> [mailto:programming-bounces at jsoftware.com] On Behalf Of John Randall
> Sent: Sunday, December 10, 2006 11:02 AM
> To: Programming forum
> Subject: Re: [Jprogramming] detecting cycles
>
> Miodrag Milenkovic wrote:
> > I know that adverb ^:_ can be used to find a fixed point of a verb.
> > But what if the application of successive powers of a verb
> tends to a
> > cycle instead of a single fixed point? What would be the conjunction
> > c, which takes as the right argument the integer n, so that
> > v c n x
> > gives the cycle of length at most n, if the sequence of successive
> > powers of v applied to x tends to such a cycle?
>
>
> If you don't want to explicitly keep track of intermediate
> results, note that
>
> (v^:(*./>:i.n))^:_ x
>
> is a cycle element.
>
> John
>
> ----------------------------------------------------------------------
> For information about J forums see
> http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list