Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,28 @@
# For n>=2, Q(n) = Q(n - Q(n-1)) + Q(n - Q(n-2))
def Q:
def Q(n):
n as $n
| (if . == null then [1,1,1] else . end) as $q
| if $q[$n] != null then $q
else
$q | Q($n-1) as $q1
| $q1 | Q($n-2) as $q2
| $q2 | Q($n - $q2[$n - 1]) as $q3 # Q(n - Q(n-1))
| $q3 | Q($n - $q3[$n - 2]) as $q4 # Q(n - Q(n-2))
| ($q4[$n - $q4[$n-1]] + $q4[$n - $q4[$n -2]]) as $ans
| $q4 | setpath( [$n]; $ans)
end ;
. as $n | null | Q($n) | .[$n];
# count the number of times Q(i) > Q(i+1) for 0 < i < n
def flips(n):
(reduce range(3; n) as $n
([1,1,1]; . + [ .[$n - .[$n-1]] + .[$n - .[$n - 2 ]] ] )) as $q
| reduce range(0; n) as $i
(0; . + (if $q[$i] > $q[$i + 1] then 1 else 0 end)) ;
# The three tasks:
((range(0;11), 1000) | "Q(\(.)) = \( . | Q)"),
(100000 | "flips(\(.)) = \(flips(.))")

View file

@ -0,0 +1,20 @@
$ uname -a
Darwin Mac-mini 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun 3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
$ time jq -r -n -f hofstadter.jq
Q(0) = 1
Q(1) = 1
Q(2) = 1
Q(3) = 2
Q(4) = 3
Q(5) = 3
Q(6) = 4
Q(7) = 5
Q(8) = 5
Q(9) = 6
Q(10) = 6
Q(1000) = 502
flips(100000) = 49798
real 0m0.562s
user 0m0.541s
sys 0m0.011s