Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit: Solved!</strong></p> <p>You can create a generator that outputs the numbers in sorted order. There are a few rules for comparing concatenated strings that I think most of us know implicitly:</p> <ul> <li>a &lt; a+b, where b is non-null.</li> <li>a+b &lt; a+c, where b &lt; c.</li> <li>a+b &lt; c+d, where a &lt; c, and a is not a subset of c.</li> </ul> <p>If you start with a sorted list of the first 1000 numbers, you can easily generate the rest by appending "thousand" or "million" and concatenating another group of 1000.</p> <p>Here's the full code, in Python:</p> <pre><code>import heapq first_thousand=[('', 0), ('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('ten', 10), ('eleven', 11), ('twelve', 12), ('thirteen', 13), ('fourteen', 14), ('fifteen', 15), ('sixteen', 16), ('seventeen', 17), ('eighteen', 18), ('nineteen', 19)] tens_name = (None, 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy','eighty','ninety') for number in range(20, 100): name = tens_name[number/10] + first_thousand[number%10][0] first_thousand.append((name, number)) for number in range(100, 1000): name = first_thousand[number/100][0] + 'hundred' + first_thousand[number%100][0] first_thousand.append((name, number)) first_thousand.sort() def make_sequence(base_generator, suffix, multiplier): prefix_list = [(name+suffix, number*multiplier) for name, number in first_thousand[1:]] prefix_list.sort() for prefix_name, base_number in prefix_list: for name, number in base_generator(): yield prefix_name + name, base_number + number return def thousand_sequence(): for name, number in first_thousand: yield name, number return def million_sequence(): return heapq.merge(first_thousand, make_sequence(thousand_sequence, 'thousand', 1000)) def billion_sequence(): return heapq.merge(million_sequence(), make_sequence(million_sequence, 'million', 1000000)) def solve(stopping_size = 51000000000): total_chars = 0 total_sum = 0 for name, number in billion_sequence(): total_chars += len(name) total_sum += number if total_chars &gt;= stopping_size: break return total_chars, total_sum, name, number </code></pre> <p>It took a while to run, about an hour. The 51 billionth character is the last character of sixhundredseventysixmillionsevenhundredfortysixthousandfivehundredseventyfive, and the sum of the integers to that point is 413,540,008,163,475,743.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload