Numbers k such that the last letter of k is the same as the first letter of k+1: Difference between revisions

m
Promoted to 'full' task.
m (→‎{{header|Phix}}: added results to 1e8th)
m (Promoted to 'full' task.)
 
(3 intermediate revisions by 3 users not shown)
Line 1:
{{draft task}}
 
;Definition
Line 168:
8: ============================================================ (299990)
9: ====================== (111301)
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
 
std::string cardinal(int n) {
static const char* small[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
};
static const char* tens[] = {
"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety",
};
assert(n >= 0 && n < 1000);
std::string str;
if (n < 20)
str = small[n];
else if (n < 100) {
str = tens[n / 10 - 2];
if (n % 10 != 0) {
str += "-";
str += small[n % 10];
}
} else {
str = cardinal(n / 100);
str += " hundred";
if (n % 100 != 0) {
str += " ";
str += cardinal(n % 100);
}
}
return str;
}
 
class a363659_generator {
public:
explicit a363659_generator() : num(0) {
for (int i = 0; i < 1000; ++i) {
std::string name = cardinal(i);
first[i] = name.front();
last[i] = name.back();
}
}
int next() {
while (first_char(num + 1) != last_char(num))
++num;
return num++;
}
 
private:
char first_char(int n) const {
int i = 0;
for (; n > 0; n /= 1000)
i = n % 1000;
return first[i];
}
char last_char(int n) const {
int i = n % 1000;
if (i > 0)
return last[i];
else if (n == 0)
return last[0];
else if (n % 1000000 == 0)
return 'n';
return 'd';
}
int num;
char first[1000];
char last[1000];
};
 
template <typename Iterator>
void histogram(Iterator begin, Iterator end) {
if (begin == end)
return;
double max_value = *std::max_element(begin, end);
const int width = 60;
int i = 0;
for (Iterator it = begin; it != end; ++it) {
std::cout << i++ << ": ";
double value = *it;
int n = max_value != 0 ? std::lround((value * width) / max_value) : 0;
int j = 0;
for (; j < n; ++j)
std::cout << u8"\u2586";
for (; j < width; ++j)
std::cout << ' ';
std::cout << ' ' << *it << '\n';
}
}
 
int main() {
std::cout << "First 50 numbers:\n";
a363659_generator gen;
int count[10] = {};
int i = 1, n;
for (; i <= 50; ++i) {
n = gen.next();
++count[n % 10];
std::cout << std::setw(3) << n << (i % 10 == 0 ? '\n' : ' ');
}
for (int limit = 1000; limit <= 1000000; limit *= 10) {
for (; i <= limit; ++i) {
n = gen.next();
++count[n % 10];
}
std::cout << "\nThe " << limit << "th number is " << n << ".\n";
std::cout << "Breakdown by last digit of first " << limit
<< " numbers:\n";
histogram(count, count + 10);
}
}</syntaxhighlight>
 
{{out}}
<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>
 
Line 978 ⟶ 1,159:
█████████████████████████ 111301 9
</pre>
 
=={{header|Raku}}==
{{trans|Wren}}
<syntaxhighlight lang="raku" line># 20230713 Raku programming solution
 
###### https://rosettacode.org/wiki/Number_names#Raku
 
constant @I = <zero one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen>;
constant @X = <0 X twenty thirty forty fifty sixty seventy eighty ninety>;
constant @C = @I X~ ' hundred';
constant @M = (<0 thousand>,
((<m b tr quadr quint sext sept oct non>,
(map { ('', <un duo tre quattuor quin sex septen octo novem>).flat X~ $_ },
<dec vigint trigint quadragint quinquagint sexagint septuagint octogint nonagint>),
'cent').flat X~ 'illion')).flat;
 
sub int-name ($num) {
if $num.substr(0,1) eq '-' { return "negative {int-name($num.substr(1))}" }
if $num eq '0' { return @I[0] }
my $m = 0;
return join ', ', reverse gather for $num.flip.comb(/\d ** 1..3/) {
my ($i,$x,$c) = .comb».Int;
if $i or $x or $c {
take join ' ', gather {
if $c { take @C[$c] }
if $x and $x == 1 { take @I[$i+10] }
else {
if $x { take @X[$x] }
if $i { take @I[$i] }
}
take @M[$m] // die "WOW! ZILLIONS!\n" if $m;
}
}
$m++;
}
}
######
 
my ($i, $c, $limit, $prev, @nums, @lastDigs) = 0, 0, 1000, int-name(0);
 
while $limit <= 1e4 {
my $next = int-name $i+1;
if $prev.substr(*-1) eq $next.substr(0,1) {
if ($c < 50) { @nums.append: $i };
@lastDigs[$i % 10] += 1;
$c++;
if $c == 50 {
say "First 50 numbers:";
say [~] $_>>.fmt('%4s') for @nums.rotor(10);
say();
} elsif $c == $limit {
print "The {$c}th number is $i.\n";
say "Breakdown by last digit of first {$c}th numbers";
say 'N Freq';
for 0..9 -> $d {
say "$d {@lastDigs[$d].fmt('%4s')} ",
'█' x (@lastDigs[$d]/@lastDigs.max*72).Int;
}
say();
$limit *= 10
}
}
$prev = $next;
$i++;
}</syntaxhighlight>
You may [https://ato.pxeger.com/run?1=bVbBbttGEL0W_IoJw4CkLFFUm6CtJQtGUwQQkDhAWyBGFSOgyJW0tbgrL5eWVIM59wtyySWH9it662f02C_pzC4pUbYJmTu7nHnzduYtzc9_quS6_PLlr1LPe9_9-5X71Fyw1HpdnPb7ShZM6ySVGYukWvQ3_Jr3L8p8xtQHkeSsePoTxjtOKkWhE6HhfAJnMPqdKQlSMMBLb6QZloqZ-VyWCozBb81CwbdmXrBbJozF-GKpyRBcMAeOL01ObGWc9YatEEQvudIM54RtDT43I2LbkdyNZbDJIGwyxsMW-0tiH1OayzrbBuN2NsPOsLejybCz7O1oMuwa9ruavd4d4b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<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Name, Fmt
import "./math" for Nums
 
9,476

edits