Boustrophedon transform: Difference between revisions
Content added Content deleted
(→{{header|jq}}: two stretch tasks) |
|||
Line 480: | Line 480: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{Works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
'''Works with jq, the C implementation of jq, within the limits of IEEE 764 arithmetic''' |
|||
'''Adapted from [[#Wren|Wren]]''' |
'''Adapted from [[#Wren|Wren]]''' |
||
The results for the "stretch" tasks are based on the use of gojq. |
|||
<syntaxhighlight lang="jq"> |
<syntaxhighlight lang="jq"> |
||
### Generic functions |
### Generic functions |
||
Line 500: | Line 502: | ||
# An array of length specified by . |
# An array of length specified by . |
||
def array($value): [range(0; .) | $value]; |
def array($value): [range(0; .) | $value]; |
||
# Give a glimpse of the (very large) input number |
|||
def glimpse: |
|||
tostring |
|||
| "\(.[:20]) ... \(.[-20:]) \(length) digits"; |
|||
### The Boustrophedon transform |
### The Boustrophedon transform |
||
Line 506: | Line 513: | ||
| ($k | array(0)) as $list |
| ($k | array(0)) as $list |
||
| {b: $list, |
| {b: $list, |
||
cache: [ |
cache: [] } |
||
⚫ | |||
# input { |
# input: {cache}, output: {result, cache} |
||
| def T($k; $n): |
| def T($k; $n): |
||
if $n == 0 then .result = $a[k] |
if $n == 0 then .result = $a[k] |
||
else .cache[$k][$n] as $kn |
|||
| if $kn > 0 then .result = $kn |
|||
else T($k; $n-1) |
|||
| |
| .result as $r |
||
| ($ |
| T($k-1; $k-$n) |
||
| . |
| .result = $r + .result |
||
| . |
| .cache[$k][$n] = .result |
||
end |
|||
end; |
end; |
||
⚫ | |||
reduce range(1; $k) as $n (.; |
| reduce range(1; $k) as $n (.; T($n; $n) | .b[$n] = .result ) |
||
| .b ; |
|||
| .b; |
|||
### Exercises |
### Exercises |
||
Line 541: | Line 549: | ||
"\nFactorials:", |
"\nFactorials:", |
||
boustrophedon([limit(15; factorials)]) |
boustrophedon([limit(15; factorials)]), |
||
## Stretch tasks require gojq |
|||
"\nGlimpse of 1000th element for the Fibonaccis", |
|||
(boustrophedon([limit(1000; fibs)]) | .[999] | glimpse), |
|||
"\nGlimpse of 1000th element for the factorials:", |
|||
(boustrophedon([limit(1000; factorials)]) | .[999] | glimpse) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 562: | Line 577: | ||
Factorials: |
Factorials: |
||
[1,2,5,17,73,381,2347,16701,134993,1222873,12279251,135425553,1627809401,21183890469,296773827547] |
[1,2,5,17,73,381,2347,16701,134993,1222873,12279251,135425553,1627809401,21183890469,296773827547] |
||
⚫ | |||
Glimpse of the 1000th element for the Fibonaccis: |
|||
56757474139659741321 ... 66135597559209657242 2370 digits |
|||
Glimpse of 1000th element for the factorials: |
|||
13714256926920345740 ... 19230014799151339821 2566 digits |
|||
⚫ | |||
=={{header|Julia}}== |
=={{header|Julia}}== |