Cycle detection: Difference between revisions

Line 1,281:
<pre>Cycle length: 6
Cycle: 101 2 5 26 167 95</pre>
 
=={{header|jq}}==
{{trans|Julia}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<lang jq>def floyd(f; x0):
{tort: (x0|f)}
| .hare = (.tort|f)
| until( .tort == .hare;
.tort |= f
| .hare |= (f|f) )
| .mu = 0
| .tort = x0
| until( .tort == .hare;
.tort |= f
| .hare |= f
| .mu += 1)
| .lambda = 1
| .hare = (.tort|f)
| until (.tort == .hare;
.hare |= f
|.lambda += 1 )
| {lambda, mu} ;
 
def f: (.*. + 1) % 255;
 
def task(f; x0):
def skip($n; stream):
foreach stream as $s (0; .+1; select(. > $n) | $s);
 
floyd(f; x0)
| .,
"Cycle:",
skip(.mu; limit((.lambda + .mu); 3 | recurse(f)));
 
task(f;3)
</lang>
{{out}}
<pre>
{
"lambda": 6,
"mu": 2
}
Cycle:
3
10
101
2
5
26
167
95
</pre>
 
=={{header|Julia}}==
2,442

edits