Upside-down numbers: Difference between revisions
Content added Content deleted
(→{{header|Free Pascal}}: up to High(Uint64)-1 = 18446744073709551614.th) |
|||
Line 135: | Line 135: | ||
5,000,000th : 82,485,246,852,682 |
5,000,000th : 82,485,246,852,682 |
||
50,000,000th : 9,285,587,463,255,281 |
50,000,000th : 9,285,587,463,255,281 |
||
</pre> |
|||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq, and with fq''' |
|||
The following solution relies on the built-in integer arithmetic capabilities of the selected implementation of jq. In the case of the C implementation, this precludes in principle the completion of the stretch task. In any case, the program below becomes very slow after the 50,000th upside-down number is generated, whichever of the above-mentioned implementations of jq is used. |
|||
<syntaxhighlight lang=jq> |
|||
# Output an unbounded stream of upside-down numbers |
|||
def genUpsideDown: |
|||
def wrappings: [ |
|||
[1, 9], [2, 8], [3, 7], [4, 6], [5, 5], |
|||
[6, 4], [7, 3], [8, 2], [9, 1] ]; |
|||
{ evens: [19, 28, 37, 46, 55, 64, 73, 82, 91], |
|||
odds: [5], |
|||
oddIndex: 0, evenIndex: 0, ndigits: 1, pow: 100 } |
|||
| while (true; |
|||
.emit = null |
|||
| if .ndigits % 2 == 1 |
|||
then if (.odds|length) > .oddIndex |
|||
then .emit = .odds[.oddIndex] |
|||
| .oddIndex += 1 |
|||
else # build next odds, but switch to evens |
|||
.nextOdds = [] |
|||
| reduce wrappings[] as $w (.; |
|||
reduce .odds[] as $i (.; |
|||
.nextOdds += [$w[0] * .pow + $i * 10 + $w[1]] ) ) |
|||
| .odds = .nextOdds |
|||
| .ndigits += 1 |
|||
| .pow *= 10 |
|||
| .oddIndex = 0 |
|||
end |
|||
elif (.evens|length) > .evenIndex |
|||
then .emit = .evens[.evenIndex] |
|||
| .evenIndex += 1 |
|||
else # build next evens, but switch to odds |
|||
.nextEvens = [] |
|||
| reduce wrappings[] as $w (.; |
|||
reduce .evens[] as $i (.; |
|||
.nextEvens += [$w[0] * .pow + $i * 10 + $w[1]] ) ) |
|||
| .evens = .nextEvens |
|||
| .ndigits += 1 |
|||
| .pow *= 10 |
|||
| .evenIndex = 0 |
|||
end ) |
|||
| select(.emit).emit ; |
|||
def task($limit): |
|||
{ count:0, ud50s: [], pow: 50 } |
|||
| foreach limit($limit; genUpsideDown) as $n (.; |
|||
.emit = null |
|||
| .count += 1 |
|||
| if .count < 50 |
|||
then .ud50s += [$n] |
|||
elif .count == 50 |
|||
then .emit = {"First 50 upside down numbers:": (.ud50s + [$n]) } |
|||
| .pow = 500 |
|||
elif .count == .pow |
|||
then .emit = {pow, $n} |
|||
| .pow *= 10 |
|||
else . |
|||
end) |
|||
| select(.emit).emit; |
|||
task(5000000) |
|||
</syntaxhighlight> |
|||
{{ouput}} |
|||
The jq and gojq programs were terminated after the results shown below were obtained. |
|||
<pre> |
|||
{"First 50 upside down numbers:":[5,19,28,37,46,55,64,73,82,91,159,258,357,456,555,654,753,852,951,1199,1289,1379,1469,1559,1649,1739,1829,1919,2198,2288,2378,2468,2558,2648,2738,2828,2918,3197,3287,3377,3467,3557,3647,3737,3827,3917,4196,4286,4376,4466]} |
|||
{"pow":500,"n":494616} |
|||
{"pow":5000,"n":56546545} |
|||
{"pow":50000,"n":6441469664} |
|||
</pre> |
</pre> |
||