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
(Python example)
Line 312: Line 312:
└ ┘
└ ┘
Frequency
Frequency
</pre>

=={{header|Python}}==
<syntaxhighlight lang="python">""" rosettacode.org, wiki task regarding OEIS sequence A363659 """

from collections import Counter
from ascii_graph import Pyasciigraph
from num2words import num2words


spelledcache = [num2words(n) for n in range(1000)]


def nonzerogroupings(n):
""" Return groupings of a thousand if not 0, least significant first. The word
representation of integers is via nonzero groups of three consecutive digits
except for numbers > 1000 which are divisible by 1000 """
groups = []
if n < 1: # exception for zero, return array containing zero
return [n]
while n > 0:
n, r = divmod(n, 1000)
if r > 0:
groups.append(r)
return groups


def firstletter(n):
return spelledcache[nonzerogroupings(n)[-1]][0]


def lastletter(n):
return num2words(n)[-1] if n > 1000 and n % 1000 == 0 \
else spelledcache[nonzerogroupings(n)[0]][-1]

def qualifies(n):
return lastletter(n) == firstletter(n + 1)


def testqualifies():
""" Test the generation of OEIS sequence A363659 """
ncount = 0
lastdigits = Counter()
print("First 50 qualifying numbers:")
for num in range(1_000_000_000):
if qualifies(num):
ncount += 1
lastdigits.update([num % 10])
if ncount < 51:
print(f'{num:5}', end='\n' if ncount % 10 == 0 else '')
elif ncount in [10**3, 10**4, 10**5, 10**6]:
print(f'\nThe {num2words(ncount)}th number is {num:,}.')
print('Breakdown by last digit of the qualifiers up to this:')
graph = Pyasciigraph()
for line in graph.graph('Frequencies', sorted(lastdigits.items())):
print(line)
print()

if ncount == 1_000_000:
break


if __name__ == '__main__':

testqualifies()
</syntaxhighlight>{{out}}
<pre>
First 50 qualifying 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 one thousandth number is 10,988.
Breakdown by last digit of the qualifiers up to this:
Frequencies
###############################################################################
██ 12 0
██████████████████████████ 111 1
█████████████████████████ 110 2
██████████████████████████ 111 3
██ 11 4
██████████████████████████ 111 5
██ 11 6
██████████████████████████ 111 7
███████████████████████████████████████████████████████████████████████ 301 8
██████████████████████████ 111 9


The ten thousandth number is 106,652.
Breakdown by last digit of the qualifiers up to this:
Frequencies
###############################################################################
███ 122 0
██████████████████████████████████████ 1301 1
████████████████████████ 829 2
██████████████████████████████████████ 1301 3
███ 121 4
██████████████████████████████████████ 1301 5
███ 121 6
███████████████████████████████████ 1211 7
██████████████████████████████████████████████████████████████████████ 2392 8
██████████████████████████████████████ 1301 9


The one hundred thousandth number is 1,095,542.
Breakdown by last digit of the qualifiers up to this:
Frequencies
###############################################################################
███ 1122 0
████████████████████████████████████ 11301 1
████████████████████████████████████████████████████████████ 18829 2
████████████████████████████████████ 11301 3
███ 1121 4
████████████████████████████████████ 11301 5
███ 1121 6
████████████████████████████████████ 11211 7
█████████████████████████████████████████████████████████████████████ 21392 8
████████████████████████████████████ 11301 9


The one millionth number is 10,984,428.
Breakdown by last digit of the qualifiers up to this:
Frequencies
###############################################################################
██ 11123 0
█████████████████████████ 111301 1
████████████████████████ 110230 2
█████████████████████████ 111301 3
██ 11121 4
█████████████████████████ 111301 5
██ 11121 6
█████████████████████████ 111211 7
████████████████████████████████████████████████████████████████████ 299990 8
█████████████████████████ 111301 9
</pre>
</pre>