Upside-down numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (New draft task and Raku example) |
|||
Line 28: | Line 28: | ||
;* [https://www.numbersaplenty.com/set/upside-down_number/ Numbers-A-Plenty: Upside-down number] |
;* [https://www.numbersaplenty.com/set/upside-down_number/ Numbers-A-Plenty: Upside-down number] |
||
;* [[oeis:A299539|OEIS:A299539 - Upside-down numbers]] |
;* [[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> |
|||
Revision as of 08:25, 14 February 2023
Upside-down numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
An upside-down number is a positive base 10 integer whose i-th leftmost and i-th rightmost digits are complements, i.e., their sum is 10.
- For example
7165493 is an upside-down number. 7 + 3 = 10 1 + 9 = 10 6 + 4 = 10 5 + 5 = 10
From that definition it follows that an upside-down number cannot contain any zeros, and if there is an odd number of digits, then the center digit must be a 5.
- Task
- Write a routine to find (or generate) upside-down numbers.
- Find and show the first 50 upside-down numbers.
- Find and show the five hundredth upside-down number.
- Find and show the five thousandth upside-down number.
- Stretch
- Find and show the fifty thousandth, five hundred thousandth, five millionth upside-down number.
- See also
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
- Output:
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
Raku
use Lingua::EN::Numbers;
sub udgen (@r) {
my @u = @r.hyper.map: { next if .contains: 0; ($_, (10 «-« .flip.comb).join) };
@u».join, @u».join(5)
}
my @upside-downs = lazy flat 5, (^∞).map({ udgen exp($_,10) .. exp(1+$_,10) });
say "First fifty upside-downs:\n" ~ @upside-downs[^50].batch(10)».fmt("%4d").join: "\n";
say '';
for 5e2, 5e3, 5e4, 5e5, 5e6 {
say "{.Int.&ordinal.tc}: " ~ comma @upside-downs[$_-1]
}
- Output:
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