Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-1.jq
Normal file
12
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-1.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def until(cond; update):
|
||||
def _until:
|
||||
if cond then . else (update | _until) end;
|
||||
try _until catch if .== "break" then empty else . end ;
|
||||
|
||||
def gcd(a; b):
|
||||
# subfunction expects [a,b] as input
|
||||
# i.e. a ~ .[0] and b ~ .[1]
|
||||
def rgcd: if .[1] == 0 then .[0]
|
||||
else [.[1], .[0] % .[1]] | rgcd
|
||||
end;
|
||||
[a,b] | rgcd ;
|
||||
18
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-2.jq
Normal file
18
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-2.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# If n is non-negative, then A002487(n)
|
||||
# generates an array with at least n elements of
|
||||
# the A002487 sequence;
|
||||
# if n is negative, elements are added until (-n)
|
||||
# is found.
|
||||
def A002487(n):
|
||||
[0,1]
|
||||
| until(
|
||||
length as $l
|
||||
| if n >= 0 then $l >= n
|
||||
else .[$l-1] == -n
|
||||
end;
|
||||
length as $l
|
||||
| ($l / 2) as $n
|
||||
| .[$l] = .[$n]
|
||||
| if (.[$l-2] == -n) then .
|
||||
else .[$l + 1] = .[$n] + .[$n+1]
|
||||
end ) ;
|
||||
31
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-3.jq
Normal file
31
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-3.jq
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# Generate a stream of n integers beginning with 1,1...
|
||||
def stern_brocot(n): A002487(n+1) | .[1:n+1][];
|
||||
|
||||
# Return the index (counting from 1) of n in the
|
||||
# sequence starting with 1,1,...
|
||||
def stern_brocot_index(n):
|
||||
A002487(-n) | length -1 ;
|
||||
|
||||
def index_task:
|
||||
(range(1;11), 100) as $i
|
||||
| "index of \($i) is \(stern_brocot_index($i))" ;
|
||||
|
||||
def gcd_task:
|
||||
A002487(1000)
|
||||
| . as $A
|
||||
| reduce range(0; length-1) as $i
|
||||
( [];
|
||||
gcd( $A[$i]; $A[$i+1] ) as $gcd
|
||||
| if $gcd == 1 then . else . + [$gcd] end)
|
||||
| if length == 0 then "GCDs are all 1"
|
||||
else "GCDs include \(.)" end ;
|
||||
|
||||
|
||||
"First 15 integers of the Stern-Brocot sequence",
|
||||
"as defined in the task description are:",
|
||||
stern_brocot(15),
|
||||
"",
|
||||
"Using an index origin of 1:",
|
||||
index_task,
|
||||
"",
|
||||
gcd_task
|
||||
33
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-4.jq
Normal file
33
Task/Stern-Brocot-sequence/Jq/stern-brocot-sequence-4.jq
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
$ jq -r -n -f stern_brocot.jq
|
||||
First 15 integers of the Stern-Brocot sequence
|
||||
as defined in the task description are:
|
||||
1
|
||||
1
|
||||
2
|
||||
1
|
||||
3
|
||||
2
|
||||
3
|
||||
1
|
||||
4
|
||||
3
|
||||
5
|
||||
2
|
||||
5
|
||||
3
|
||||
4
|
||||
|
||||
Using an index origin of 1:
|
||||
index of 1 is 1
|
||||
index of 2 is 3
|
||||
index of 3 is 5
|
||||
index of 4 is 9
|
||||
index of 5 is 11
|
||||
index of 6 is 33
|
||||
index of 7 is 19
|
||||
index of 8 is 21
|
||||
index of 9 is 35
|
||||
index of 10 is 39
|
||||
index of 100 is 1179
|
||||
|
||||
GCDs are all 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue