Numbers k such that the last letter of k is the same as the first letter of k+1: Difference between revisions
Content added Content deleted
m (→{{header|Julia}}: up to a trillion) |
|||
Line 318: | Line 318: | ||
8 2392 |
8 2392 |
||
9 1301</syntaxhighlight> |
9 1301</syntaxhighlight> |
||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Also works with gojq, the Go implementation of jq.''' |
|||
The following is probably mainly of interest because of the utility functions: |
|||
* lpad # left pad |
|||
* rpad # right pad |
|||
* tprint # tabular print |
|||
* whilst # control structure |
|||
* number2name # English name of a number |
|||
* barChart # horizontal barchart |
|||
'''Preliminaries''' |
|||
<syntaxhighlight lang="jq"> |
|||
# The following may be omitted if using the C implementation of jq |
|||
def _nwise($n): |
|||
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
|||
n; |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .; |
|||
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l); |
|||
# tabular print |
|||
def tprint(columns; wide): |
|||
reduce _nwise(columns) as $row (""; |
|||
. + ($row|map(lpad(wide)) | join(" ")) + "\n" ); |
|||
# If cond then emit f and use that value to recurse |
|||
def whilst(cond; f): def r: select(cond) | f | (., r); r; |
|||
# Input: a positive or negative number for which tostring prints with only characters from [-.0-9] |
|||
# Output: the name of the number, e.g. "zero" for 0 |
|||
# An error is raised if the above-mentioned requirement is not met. |
|||
def number2name: |
|||
def __small: [ |
|||
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", |
|||
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ]; |
|||
def __tens: ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; |
|||
def __illions: [ |
|||
"", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion", |
|||
" sextillion", " septillion", " octillion", " nonillion", "decillion" ]; |
|||
. as $n |
|||
| if tostring | test("[eE]") then "number2name cannot handle \(.)" | error else . end |
|||
| false as $_uk # United Kingdom mode |
|||
| "minus" as $_neg |
|||
| "point" as $_point |
|||
| {f: "", |
|||
and: (if $_uk then "and " else "" end) } |
|||
| if $n < 0 |
|||
then .t = $_neg + " " |
|||
| .n = - $n |
|||
else .n = $n |
|||
end |
|||
| if (.n | (. != floor)) |
|||
then .f = (.n | tostring | sub("^[0-9]*[.]";"")) |
|||
| .n |= trunc |
|||
else . |
|||
end |
|||
| if .n < 20 |
|||
then .t += __small[.n] |
|||
elif .n < 100 |
|||
then .t += __tens[(.n/10)|floor] |
|||
| (.n % 10) as $s |
|||
| if $s > 0 then .t += "-" + __small[$s] else . end |
|||
elif .n < 1000 |
|||
then .t += __small[(.n/100)|floor] + " hundred" |
|||
| (.n % 100) as $s |
|||
| if $s > 0 then .t += " " + .and + ($s|number2name) else . end |
|||
else .sx = "" |
|||
| .i = 0 |
|||
| until (.n <= 0; |
|||
(.n % 1000) as $p |
|||
| .n |= ((./1000)|floor) |
|||
| if $p > 0 |
|||
then .ix = ($p|number2name) + __illions[.i] |
|||
| if .sx != "" then .ix += " " + .sx else . end |
|||
| .sx = .ix |
|||
else . |
|||
end |
|||
| .i += 1) |
|||
| .t += .sx |
|||
end |
|||
| if .f != "" |
|||
then .t += " " + $_point |
|||
| reduce (.f | explode[] | [.] | implode | tonumber) as $d |
|||
(.; .t += " \(__small[$d])" ) |
|||
else . |
|||
end |
|||
| .t ; |
|||
# Draws a horizontal bar chart on the terminal representing an array |
|||
# of numerical 'data', which must be non-negative, for example: |
|||
# Title |
|||
# -------------------------------------------------- |
|||
# a □ 0 |
|||
# bb ■■■■■■■■■■■■■■■■■■■■■■ 1 |
|||
# ccc ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 2 |
|||
# -------------------------------------------------- |
|||
# 'labels' is a list of the corresponding text for each bar. |
|||
# If 'rjust' is true then the labels are right-justified within their maximum length, |
|||
# otherwise they are left-justified. |
|||
# 'width' is the desired total width of the chart in characters. |
|||
# 'symbol' is the character to be used to draw each bar, |
|||
# 'symbol2' is used to represent scaled non-zero data and 'symbol3' zero data which |
|||
# would otherwise be blank. The actual data is printed at the end of each bar. |
|||
# |
|||
def barChart ($title; $width; $labels; $data; $rjust; $symbol; $symbol2; $symbol3): |
|||
def times($n): if $n > 0 then . * $n else "" end; |
|||
def n: if isinfinite then "∞" else tostring end; |
|||
($labels|length) as $barCount |
|||
| if ($data|length) != $barCount then "Mismatch between labels and data." | error else . end |
|||
| ($labels | map(length) | max) as $maxLabelLen |
|||
| ($labels |
|||
| if $rjust |
|||
then map( lpad( $maxLabelLen ) ) |
|||
else map( rpad( $maxLabelLen ) ) |
|||
end ) as $labels |
|||
| ($data|max) as $maxData |
|||
| ($data|map(if isinfinite then 1 else tostring|length end)|max) as $maxDataLength |
|||
| ($width - $maxLabelLen - $maxDataLength - 2) as $maxLen # maximum length of a bar |
|||
| ($data | map( if isinfinite then $maxLen else ((. * $maxLen / $maxData)|floor) end)) as $scaledData |
|||
| ( "-" * ([$width, ($title|length)] | max)) as $dashes |
|||
| $title, |
|||
$dashes, |
|||
(range(0; $barCount) as $i |
|||
| ($symbol | times($scaledData[$i])) |
|||
| if . == "" then (if $data[$i] > 0 then $symbol2 else $symbol3 end) else . end |
|||
| "\($labels[$i]) \(.) \($data[$i]|n)" ), |
|||
$dashes ; |
|||
# Convenience version of the above function using default symbols: |
|||
def barChart($title; $width; $labels; $data; $rjust): |
|||
barChart($title; $width; $labels; $data; $rjust; "■"; "◧"; "□"); |
|||
# Convenience version using right justification for labels. |
|||
def barChart($title; $width; $labels; $data): |
|||
barChart($title; $width; $labels; $data; true; "■"; "◧"; "□"); |
|||
</syntaxhighlight> |
|||
'''The task''' |
|||
<syntaxhighlight lang="jq"> |
|||
def task($limit): |
|||
[range(0;10) | tostring] as $labels |
|||
| {i: 0, |
|||
c: 0, |
|||
nums: [], |
|||
lastDigs: [range(0;10)|0], |
|||
prev: "zero", |
|||
limit: 1000 } |
|||
| whilst (.limit <= $limit; |
|||
.emit = null |
|||
| (.i+1 | number2name) as $next |
|||
| if .prev[-1:] == $next[:1] |
|||
then if (.c < 50) then .nums += [.i] else . end |
|||
| .lastDigs[.i % 10] += 1 |
|||
| .c += 1 |
|||
| if .c == 50 |
|||
then .emit = ["First 50 numbers:", (.nums|tprint(10;3))] |
|||
elif .c == .limit |
|||
then .emit = ["\nThe \(.c)th number is \(.i)\n"] |
|||
| "Breakdown by last digit of first \(.c) numbers" as $title |
|||
| .emit += [barChart($title; 80; $labels; .lastDigs)] |
|||
| .limit *= 10 |
|||
else . |
|||
end |
|||
else . |
|||
end |
|||
| .prev = $next |
|||
| .i += 1 ) |
|||
| select(.emit).emit[] ; |
|||
task(1e6) |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
First 50 numbers: |
|||
0 18 28 38 79 81 83 85 97 102 |
|||
122 132 142 152 162 172 182 192 208 228 |
|||
238 248 258 268 278 288 298 308 328 338 |
|||
348 358 368 378 388 398 799 801 803 805 |
|||
809 812 821 823 825 829 831 833 835 839 |
|||
The 1000th number is 10988 |
|||
Breakdown by last digit of first 1000 numbers |
|||
-------------------------------------------------------------------------------- |
|||
0 ■■ 12 |
|||
1 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 111 |
|||
2 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 110 |
|||
3 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 111 |
|||
4 ■■ 11 |
|||
5 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 111 |
|||
6 ■■ 11 |
|||
7 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 111 |
|||
8 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 301 |
|||
9 ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 111 |
|||
-------------------------------------------------------------------------------- |
|||
The 10000th number is 106652 |
|||
Breakdown by last digit of first 10000 numbers |
|||
-------------------------------------------------------------------------------- |
|||
0 ■■■ 122 |
|||
1 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1301 |
|||
2 ■■■■■■■■■■■■■■■■■■■■■■■■■ 829 |
|||
3 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1301 |
|||
4 ■■■ 121 |
|||
5 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1301 |
|||
6 ■■■ 121 |
|||
7 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1211 |
|||
8 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 2392 |
|||
9 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1301 |
|||
-------------------------------------------------------------------------------- |
|||
The 100000th number is 1095542 |
|||
Breakdown by last digit of first 100000 numbers |
|||
-------------------------------------------------------------------------------- |
|||
0 ■■■ 1122 |
|||
1 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11301 |
|||
2 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 18829 |
|||
3 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11301 |
|||
4 ■■■ 1121 |
|||
5 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11301 |
|||
6 ■■■ 1121 |
|||
7 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11211 |
|||
8 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 21392 |
|||
9 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11301 |
|||
-------------------------------------------------------------------------------- |
|||
The 1000000th number is 10984428 |
|||
Breakdown by last digit of first 1000000 numbers |
|||
-------------------------------------------------------------------------------- |
|||
0 ■■ 11123 |
|||
1 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 111301 |
|||
2 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 110230 |
|||
3 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 111301 |
|||
4 ■■ 11121 |
|||
5 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 111301 |
|||
6 ■■ 11121 |
|||
7 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 111211 |
|||
8 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 299990 |
|||
9 ■■■■■■■■■■■■■■■■■■■■■■■■■■ 111301 |
|||
-------------------------------------------------------------------------------- |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |