Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Recamans-sequence/Jq/recamans-sequence-1.jq
Normal file
46
Task/Recamans-sequence/Jq/recamans-sequence-1.jq
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# Let R[n] be the Recaman sequence, n >= 0, so R[0]=0.
|
||||
# Input: a number, $required, specifying the required range of integers, [1 .. $required]
|
||||
# to be covered by R[0] ... R[.n]
|
||||
# $capture: the number of elements of the sequence to retain.
|
||||
# Output: an object as described below.
|
||||
# Note that .a|length will be equal to $capture.
|
||||
#
|
||||
def recaman_required($capture):
|
||||
. as $required
|
||||
| {
|
||||
n: 0,
|
||||
current: 0, # R[.n]
|
||||
previous: null, # R[.n-1]
|
||||
a: [0], # only maintained up to a[$capture-1]
|
||||
used: { "0": true }, # hash for checking whether a value has already occurred
|
||||
found: { "0": true }, # hash for checking how many in [0 .. $required] inclusive have been found
|
||||
nfound: 1, # .found|length
|
||||
foundDup: null, # the first duplicated entry in the sequence
|
||||
foundDupAt: null # .foundDup == R[.foundDupAt]
|
||||
}
|
||||
| until ((.n >= $capture) and .foundDup and (.nfound > $required);
|
||||
.n += 1
|
||||
| .current -= .n
|
||||
| if (.current < 1 or .used[.current|tostring]) then .current = .current + 2*.n else . end
|
||||
| (.current|tostring) as $s
|
||||
| .used[$s] as $alreadyUsed
|
||||
| if .n < $capture then .a += [.current] else . end
|
||||
| if ($alreadyUsed|not)
|
||||
then .used[$s] = true
|
||||
| if (.current >= 0 and .current <= $required)
|
||||
then .found[$s] = true | .nfound+=1
|
||||
else . end
|
||||
else .
|
||||
end
|
||||
| if (.foundDup|not) and $alreadyUsed
|
||||
then .foundDup = .current
|
||||
| .foundDupAt = .n
|
||||
else .
|
||||
end );
|
||||
|
||||
1000 as $required
|
||||
| 15 as $capture
|
||||
| $required | recaman_required($capture)
|
||||
| "The first \($capture) terms of Recaman's sequence are: \(.a)",
|
||||
"The first duplicated term is a[\(.foundDupAt)] = \(.foundDup)",
|
||||
"Terms up to a[\(.n)] are needed to generate 0 to \($required) inclusive."
|
||||
34
Task/Recamans-sequence/Jq/recamans-sequence-2.jq
Normal file
34
Task/Recamans-sequence/Jq/recamans-sequence-2.jq
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# Output: the stream of elements in the Recaman sequence, beginning with 0.
|
||||
def recaman:
|
||||
0,
|
||||
foreach range(1; infinite) as $i ({used: {"0": true}, current: 0};
|
||||
(.current - $i) as $next
|
||||
| .current = (if ($next < 1 or .used[$next|tostring]) then $next + 2 * $i else $next end)
|
||||
| .used[.current|tostring] = true;
|
||||
.current );
|
||||
|
||||
# emit [.i, $x] for duplicated terms using IO==0
|
||||
def duplicated(s):
|
||||
foreach s as $x ({used: {}, i: -1};
|
||||
.i += 1
|
||||
| ($x|tostring) as $xs
|
||||
| if .used[$xs] then .emit = [.i, $x] else .used[$xs] = true end;
|
||||
select(.emit) | .emit);
|
||||
|
||||
# Input: an integer, $required
|
||||
# s: a stream of non-negative integers
|
||||
# Output: the index of the item in the stream s at which the stream up to and including
|
||||
# that item includes all integers in the closed interval [0 .. $required].
|
||||
#
|
||||
def covers(s):
|
||||
. as $required
|
||||
| first(foreach s as $x ( { i: -1, found: {}, nfound: 0};
|
||||
.i += 1
|
||||
| ($x|tostring) as $xs
|
||||
| if .found[$xs] then .
|
||||
elif $x <= $required
|
||||
then .found[$xs] = true | .nfound += 1
|
||||
| if .nfound > $required then .emit=.i else . end
|
||||
else .
|
||||
end;
|
||||
select(.emit).emit) );
|
||||
6
Task/Recamans-sequence/Jq/recamans-sequence-3.jq
Normal file
6
Task/Recamans-sequence/Jq/recamans-sequence-3.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
"First 15:", limit(15; recaman),
|
||||
|
||||
"\First duplicated:", first(duplicated(recaman)),
|
||||
|
||||
"\Index of first element to include 0 to 1000 inclusive:",
|
||||
(1000|covers(recaman))
|
||||
Loading…
Add table
Add a link
Reference in a new issue