Upside-down numbers: Difference between revisions

→‎{{header|Quackery}}: Added description of method.
m (→‎{{header|Quackery}}: tweaked layout)
(→‎{{header|Quackery}}: Added description of method.)
Line 1,078:
 
=={{header|Quackery}}==
 
(Load extensions for the faster merge sort.)
 
'''Method'''
 
Start with generation zero as the empty string and "5". Make the next generation by expanding the previous generation (<code>expand</code>) i.e. wrapping each of the strings in "1" and "9", "2" and "8" … "8" and "2", "9" and "1". Accumulate the generations. The accumulator starts with "5", after one iteration it has "5", "19", "28" … "82", "91", "159", "258" … "852", "951".
 
Note that this does not produce the numbers in numerical order. It's close to numerical order but not quite there.
So once sufficient numbers have been produced to guarantee that all the numbers in the required range have been calculated, convert them from string format to numerical format and sort, then truncate the list of upside down numbers to the required length.
 
<code>necessary</code> computes a safe number of items to include in the list to guarantee that none are missing from the truncated list - i.e. the length of the list after each iteration of <code>expand</code>. It is <code>(9^n - 5) / 4</code>, where <code>n</code> is the number of iterations. ([https://oeis.org/A211866 OEIS A211866])
 
<code>^</code> in <code>necessary</code> is bitwise XOR, not exponentiation.
 
<syntaxhighlight lang="Quackery"> [ 0
1,467

edits