Upside-down numbers: Difference between revisions

Created Nim solution.
(Applesoft BASIC)
(Created Nim solution.)
Line 509:
Five millionth: 82,485,246,852,682
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="Nim">import std/[math, strformat, strutils, sugar]
 
iterator upsideDownNumbers(): (int, int) =
## Generate upside-down numbers (OEIS A299539).
const
Wrappings = [(1, 9), (2, 8), (3, 7), (4, 6),
(5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
var
evens = @[19, 28, 37, 46, 55, 64, 73, 82, 91]
odds = @[5]
oddIndex, evenIndex = 0
ndigits = 1
count = 0
 
while true:
if ndigits mod 2 == 1:
if odds.len > oddIndex:
inc count
yield (count, odds[oddIndex])
inc oddIndex
else:
# Build next odds, but switch to evens.
odds = collect:
for (hi, lo) in Wrappings:
for i in odds:
hi * 10^(ndigits + 1) + 10 * i + lo
inc ndigits
oddIndex = 0
else:
if evens.len > evenIndex:
inc count
yield (count, evens[evenIndex])
inc evenIndex
else:
# Build next evens, but switch to odds.
evens = collect:
for (hi, lo) in Wrappings:
for i in evens:
hi * 10^(ndigits + 1) + 10 * i + lo
inc ndigits
evenIndex = 0
 
echo "First fifty upside-downs:"
for (udcount, udnumber) in upsideDownNumbers():
if udcount <= 50:
stdout.write &"{udnumber:5}"
if udcount mod 10 == 0: echo()
elif udcount == 500:
echo &"\nFive hundredth: {insertSep($udnumber)}"
elif udcount == 5_000:
echo &"Five thousandth: {insertSep($udnumber)}"
elif udcount == 50_000:
echo &"Fifty thousandth: {insertSep($udnumber)}"
elif udcount == 500_000:
echo &"Five hundred thousandth: {insertSep($udnumber):}"
elif udcount == 5_000_000:
echo &"Five millionth: {insertSep($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>
 
=={{header|Pascal}}==
256

edits