Upside-down numbers: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
m (julia example) |
||
Line 135: | Line 135: | ||
5,000,000th : 82,485,246,852,682 |
5,000,000th : 82,485,246,852,682 |
||
50,000,000th : 9,285,587,463,255,281 |
50,000,000th : 9,285,587,463,255,281 |
||
</pre> |
|||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">using Formatting |
|||
using ResumableFunctions |
|||
@resumable function gen_upsidedowns() |
|||
""" 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] |
|||
ndigits, odd_index, even_index, olen, elen = 1, 0, 0, 1, 9 |
|||
while true |
|||
if isodd(ndigits) |
|||
if olen > odd_index |
|||
@yield odds[begin + odd_index] |
|||
odd_index += 1 |
|||
else |
|||
# build next odds, but switch to evens |
|||
odds = vec([hi * 10^(ndigits + 1) + 10 * i + lo for i in odds, (hi, lo) in wrappings]) |
|||
ndigits += 1 |
|||
odd_index = 0 |
|||
olen = length(odds) |
|||
end |
|||
else |
|||
if elen > even_index |
|||
@yield evens[begin + even_index] |
|||
even_index += 1 |
|||
else |
|||
# build next evens, but switch to odds |
|||
evens = vec([hi * 10^(ndigits + 1) + 10 * i + lo for i in evens, (hi, lo) in wrappings]) |
|||
ndigits += 1 |
|||
even_index = 0 |
|||
elen = length(evens) |
|||
end |
|||
end |
|||
end |
|||
end |
|||
println("First fifty upside-downs:") |
|||
for (udcount, udnumber) in enumerate(gen_upsidedowns()) |
|||
if udcount <= 50 |
|||
print(lpad(udnumber, 5), udcount % 10 == 0 ? "\n" : "") |
|||
elseif udcount == 500 |
|||
println("\nFive hundredth: ", format(udnumber, commas = true)) |
|||
elseif udcount == 5000 |
|||
println("Five thousandth: ", format(udnumber, commas = true)) |
|||
elseif udcount == 50_000 |
|||
println("Fifty thousandth: ", format(udnumber, commas = true)) |
|||
elseif udcount == 500_000 |
|||
println("Five hundred thousandth: ", format(udnumber, commas = true)) |
|||
elseif udcount == 5_000_000 |
|||
println("Five millionth: ", format(udnumber, commas = true)) |
|||
break |
|||
end |
|||
end |
|||
</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> |
</pre> |
||