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

New post.
m (Promoted to 'full' task.)
(New post.)
 
Line 499:
8 2392
9 1301</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Arrays;
 
public final class NumberKSuchThatTheLastLetterOfKIsTheSameAsTheFirstLetterOfKPlusOne {
 
public static void main(String[] args) {
System.out.println("The first 50 numbers:");
A363659Generator generator = new A363659Generator();
int[] digitCount = new int[10];
int index = 1;
int number = 0;
while ( index <= 50 ) {
number = generator.next();
digitCount[number % 10] += 1;
System.out.print(String.format("%3d%s", number, ( index % 10 == 0 ) ? "\n" : " " ));
index += 1;
}
System.out.println();
for ( int limit = 1_000; limit <= 1_000_000; limit *= 10 ) {
while ( index <= limit ) {
number = generator.next();
digitCount[number % 10] += 1;
index += 1;
}
System.out.println("The " + limit + "th number is " + number + ".");
System.out.println("Breakdown by last digit of first " + limit + " numbers:");
drawHistogram(digitCount);
}
}
private static void drawHistogram(int[] digitCount) {
final int MAX_VALUE = Arrays.stream(digitCount).boxed().max(Integer::compare).get();
final int WIDTH = 60;
for ( int i = 0; i < digitCount.length; i++ ) {
System.out.print(i + ": ");
final int rowLength = ( MAX_VALUE != 0 ) ? ( digitCount[i] * WIDTH ) / MAX_VALUE : 0;
for ( int index = 0; index < WIDTH; index++ ) {
String toPrint = ( index < rowLength ) ? "■" : " ";
System.out.print(toPrint);
}
System.out.println(" " + digitCount[i]);
}
System.out.println();
}
}
 
final class A363659Generator {
 
public A363659Generator() {
for ( int i = 0; i < 1_000; i++ ) {
String name = NumberToWordsConverter.convert(i);
firstChar[i] = name.toLowerCase().charAt(0);
lastChar[i] = name.charAt(name.length() - 1);
}
}
public int next() {
while ( firstChar(number + 1) != lastChar(number) ) {
number += 1;
}
return number++;
}
 
private char firstChar(int n) {
int i = 0;
while ( n > 0 ) {
i = n % 1_000;
n /= 1_000;
}
return firstChar[i];
}
private char lastChar(int n) {
final int i = n % 1_000;
if ( i > 0 ) {
return lastChar[i];
}
if ( n == 0 ) {
return lastChar[0];
}
if ( n % 1_000_000 == 0 ) {
return 'n';
}
return 'd';
}
private int number = 0;
private char[] firstChar = new char[1_000];
private char[] lastChar = new char[1_000];
}
 
final class NumberToWordsConverter { // Valid for positive integers up to 9_999_999
 
public static String convert(int n) {
if ( n < 20 ) {
return units[n];
}
if ( n < 100 ) {
return tens[n / 10] + ( ( n % 10 > 0 ) ? " " + convert(n % 10) : "" );
}
if ( n < 1_000 ) {
return units[n / 100] + " Hundred" + ( ( n % 100 > 0 ) ? " and " + convert(n % 100) : "" );
}
if ( n < 1_000_000 ) {
return convert(n / 1_000) + " Thousand " + ( ( n % 1_000 > 0 ) ? " " + convert(n % 1_000) : "" );
}
return convert(n / 1_000_000) + " Million " + ( ( n % 1_000_000 > 0 ) ? " " + convert(n % 1_000_000) : "" );
}
private static final String[] units =
{ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
private static final String[] tens =
{ "","", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
}
</syntaxhighlight>
{{ out }}
<pre>
The 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|jq}}==
915

edits