Upside-down numbers: Difference between revisions

m
(New draft task and Raku example)
 
Line 28:
;* [https://www.numbersaplenty.com/set/upside-down_number/ Numbers-A-Plenty: Upside-down number]
;* [[oeis:A299539|OEIS:A299539 - Upside-down numbers]]
 
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" rosettacode.org task Upside-down_numbers """
 
 
def gen_upside_down_number():
""" generate upside-down numbers (OEIS A299539) """
wrappings = [[1, 9], [2, 8], [3, 7], [4, 6],
[5, 5], [6, 4], [7, 3], [8, 2], [9, 1]]
evens = [19, 28, 37, 46, 55, 64, 73, 82, 91]
odds = [5]
odd_index, even_index = 0, 0
ndigits = 1
while True:
if ndigits % 2 == 1:
if len(odds) > odd_index:
yield odds[odd_index]
odd_index += 1
else:
# build next odds, but switch to evens
odds = sorted([hi * 10**(ndigits + 1) + 10 *
i + lo for i in odds for hi, lo in wrappings])
ndigits += 1
odd_index = 0
else:
if len(evens) > even_index:
yield evens[even_index]
even_index += 1
else:
# build next evens, but switch to odds
evens = sorted([hi * 10**(ndigits + 1) + 10 *
i + lo for i in evens for hi, lo in wrappings])
ndigits += 1
even_index = 0
 
 
print('First fifty upside-downs:')
for (udcount, udnumber) in enumerate(gen_upside_down_number()):
if udcount < 50:
print(f'{udnumber : 5}', end='\n' if (udcount + 1) % 10 == 0 else '')
elif udcount == 499:
print(f'\nFive hundredth: {udnumber: ,}')
elif udcount == 4999:
print(f'\nFive thousandth: {udnumber: ,}')
elif udcount == 49_999:
print(f'\nFifty thousandth: {udnumber: ,}')
elif udcount == 499_999:
print(f'\nFive hundred thousandth: {udnumber: ,}')
elif udcount == 4_999_999:
print(f'\nFive millionth: {udnumber: ,}')
break
</syntaxhighlight>{{out}}
<pre>
First fifty upside-downs:
5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1199
1289 1379 1469 1559 1649 1739 1829 1919 2198 2288
2378 2468 2558 2648 2738 2828 2918 3197 3287 3377
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466
 
Five hundredth: 494,616
 
Five thousandth: 56,546,545
 
Fifty thousandth: 6,441,469,664
 
Five hundred thousandth: 729,664,644,183
 
Five millionth: 82,485,246,852,682
</pre>
 
 
4,102

edits