Hofstadter Figure-Figure sequences: Difference between revisions

m (→‎version 1: added wording concerning computation speed.)
Line 1,494:
R(10) = 69
1000 integer check ok.</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
In this entry, the functions `ffr` and `ffs` are defined as per the
task requirements, but neither is used. Instead, for efficiency, a
function for extending the two sequences is defined. This function is
then used to create generators, which are then harnessed
to accomplish the specific tasks.
 
<lang jq>def init: {r: [0, 1], s: [0, 2] };
 
# input: {r,s}
# output: {r,s,emit} where .emit is either null or the next R and where either .r or .s on output has been extended.
# .emit is provided in case an unbounded stream of R values is desired.
def extend_ff:
(.r|length) as $rn
| if .s[$rn - 1]
then .emit = .r[$rn - 1] + .s[$rn - 1]
| .r[$rn] = .emit
| reduce range( [.r[$rn-1], .s[-1]] | max + 1; .r[$rn] ) as $i (.; .s += [$i] )
else .emit = null
| .s += [.r[$rn - 1] + 1]
end;
 
def ffr($n):
first(init | while(true; extend_ff) | select(.r[$n])).r[$n] ;
 
def ffs($n):
first(init | while(true; extend_ff) | select(.s[$n])).s[$n] ;
 
def task1($n):
"The first \($n) values of R are:",
(init | until( .r | length > $n; extend_ff) | .r[1:]) ;
 
def task2:
"The result of checking that the first 40 values of R and the first 960 of S together cover the interval [1,1000] is:",
( init | until( (.r|length) > 40 and (.s|length) > 960; extend_ff)
| (.r[1:41] + .s[1:961] | sort) == [range(1;1001)] ) ;
 
task1(10), task2</lang>
{{out}}
<pre>
The first 10 values of R are:
[1,3,7,12,18,26,35,45,56,69]
The result of checking that the first 40 values of R and the first 960 of S together cover the interval [1,1000] is:
true
</pre>
 
 
=={{header|Julia}}==
2,459

edits