Most creative way to reverse a positive integer
Mathematica, no modulo!
n = 14627;
length = Ceiling[Log[10, n]];
img = Rasterize[n, RasterSize -> 400, ImageSize -> 400];
box = Rasterize[n, "BoundingBox", RasterSize -> 400, ImageSize -> 400];
width = box[[1]]; height = box[[3]];
ToExpression[
TextRecognize[
ImageAssemble[
ImageTake[img, {1, height}, #] & /@
NestList[# - width/length &, {width - width/length, width},
length - 1]]]]
Let's break it down.
First we use some "creative arithmetics" to find out how many digits are in the number: length = Ceiling[Log[10, n]];
Next, we Rasterize the number to a nice large image:
Now we query for the bounding box of that image, and populate the width and height (actually using the baseline offset instead of the image height, because MM adds some whitespace below the baseline in the image).
Next, NestList recursively subtracts the width of the image divided by the length of the string to enable ImageTake to pluck characters from the end of the image one by one, and those are reassembled by ImageAssemble to this image:
Then we pass that on to the TextRecognize function for optical character recognition, which at this image size and rasterization quality is able to impeccably recognize the final output and give us the integer:
72641
Logarithms and OCR - It's like chocolate and peanut butter!
New and improved
This version pads out the number to deal with the obstinate behavior of TextRecognize with small numbers, and then subtracts out the pad at the end. This even works for single-digit numbers!
Though, why you would run a reverse routine on a single number is a mystery to me. But just for the sake of completeness, I even made it work for inputs of zero and one, which would normally break because the floored log doesn't return 1 for them.
n = 1;
pad = 94949;
length = If[n == 1 || n == 0, 1, Ceiling[Log[10, n]]];
img = Rasterize[n + (pad*10^length), RasterSize -> 400,
ImageSize -> 400];
padlength = length + 5;
box = ImageDimensions[img];
width = box[[1]]; height = box[[2]];
reversed =
ImageResize[
ImageAssemble[
ImageTake[img, {1, height}, #] & /@
NestList[# - width/padlength &, {width + 1 - width/padlength,
width}, padlength - 1]], 200];
recognized = ToExpression[TextRecognize[reversed]];
(recognized - pad)/10^5
Perl/LuaTeX/Tesseract
The following Perl script reads the number as command line argument, e.g.:
1234567890
The following Perl script prints the number via LuaTeX. A virtual font is created on the fly that mirrors the digits horizontally.
Then the whole number is again mirrored horizontally:
The final image is reread via OCR (tesseract):
0987654321
#!/usr/bin/env perl
use strict;
$^W=1;
# Get the number as program argument or use a fixed number with all digits.
$_ = shift // 1234567890;
$\="\n"; # append EOL, when printing
# Catch negative number
exit print "NaUI (Not an Unsigned Integer)" if $_ < 0;
# Catch number with one digit.
exit ! print if ($_ = $= = $_) < 10;
undef $\;
# Write TeX file for LuaTeX
open(OUT, '>', 'temp.tex') or die "!!! Error: Cannot write: $!\n";
print OUT<<"END_PRINT";
% Catcode setting for iniTeX (a TeX format is not needed)
\\catcode`\{=1
\\catcode`\}=2
\\def\\mynumber{$_}
END_PRINT
print OUT<<'END_PRINT';
\directlua{tex.enableprimitives('',tex.extraprimitives())}
\pdfoutput=1 % PDF output
% move origin to (0,0)
\pdfhorigin=0bp
\pdfvorigin=0bp
% magnify the result by 5
\mag=5000
% Create virtual font, where the digits are mirrored
\directlua{
callback.register('define_font',
function (name,size)
if name == 'cmtt10-digits' then
f = font.read_tfm('cmtt10',size)
f.name = 'cmtt10-digits'
f.type = 'virtual'
f.fonts = {{ name = 'cmtt10', size = size }}
for i,v in pairs(f.characters) do
if (string.char(i)):find('[1234567890]') then
v.commands = {
{'right',f.characters[i].width},
{'special','pdf: q -1 0 0 1 0 0 cm'},
{'char',i},
{'right',-f.characters[i].width},
{'special','pdf: Q'},
}
else
v.commands = {{'char',i}}
end
end
else
f = font.read_tfm(name,size)
end
return f
end
)
}
% Activate the new font
\font\myfont=cmtt10-digits\relax
\myfont
% Put the number in a box and add a margin (for tesseract)
\dimen0=5bp % margin
\setbox0=\hbox{\kern\dimen0 \mynumber\kern\dimen0}
\ht0=\dimexpr\ht0+\dimen0\relax
\dp0=\dimexpr\dp0+\dimen0\relax
\pdfpagewidth=\wd0
\pdfpageheight=\dimexpr\ht0+\dp0\relax
% For illustration only: Print the number with the reflected digits:
\shipout\copy0 % print the number with the reflected digits
% Final version on page 2: Print the box with the number, again mirrored
\shipout\hbox{%
\kern\wd0
\pdfliteral{q -1 0 0 1 0 0 cm}%
\copy0
\pdfliteral{Q}%
}
% End job, no matter, whether iniTeX, plain TeX or LaTeX
\csname @@end\endcsname\end
END_PRINT
system "luatex --ini temp.tex >/dev/null";
system qw[convert temp.pdf temp%d.png];
system "tesseract temp1.png temp >/dev/null 2>&1";
# debug versions with output on console
#system "luatex --ini temp.tex";
#system qw[convert temp.pdf temp%d.png];
#system "tesseract temp1.png temp";
# Output the result, remove empty lines
open(IN, '<', 'temp.txt') or die "!!! Error: Cannot open: $!\n";
chomp, print while <IN>;
print "\n";
close(IN);
__END__
Brainfuck
Basically, it is just an input-reversing program.
,[>,]<[.<]
UPD: As Sylwester pointed out in comments, in the classical Brainfuck interpreters/compilers (without possibility to going left from zero point in the memory array) this program would not work in the absence of '>' at the beginning, so the more stable version is:
>,[>,]<[.<]