Computer: Algorithm
ReverseDigits function   (0)  [vote for, against]
A purely numerical conversion

Most computer languages offer the ability to manipulate strings of characters, including numerical-digit characters, and there is commonly available a function to take such a string and create a new string with all the same characters, but in reverse order. Thus the function processing a string like "abcde" would yield "edcba".

Some computer languages, however, don't do strings. One that I've been doing a lot of playing with recently, called "GAP" (for "Groups, Algorithms, Programming, see link) doesn't even handle non-integer ("floating-point") numbers (but it sure can handle very large integers!).

So, just for fun, here is a ReverseDigits function written in the GAP language (which strongly resembles Pascal). Note that due to the lack of line-width here, I've put the comment associated with each line of code just after the line of code (a comment begins with a # symbol). Also, keep in mind that browsers don't like text to start with blank spaces, so I've used periods to show the indenting or "structure" of this little function. The periods need to be deleted before you actually try to run this code.

#This is a "stupid trick" function. All it does is take a
# number such as 123 and reverse its digits so that the
# return value would be 321. This might rarely be useful
# when playing with large numbers that have nearly the
# same value; the reversed-digits values will almost
# always be quite different.

ReverseDigits := function(arg)
#up to 3 parameters allowed, received as a list
. local n,b,m,f,r,d;
. #declare local variables to be used
. n:=arg[1];
. #get the Number to reverse (essential!)
. d:=Length(arg);
. #prepare to examine any other parameters
. m:=0;
. #temp Magnitude/3rd-parameter (quantity of digits)
. if (d>1) then
. . b:=arg[2];
. . #2nd parameter is the controlling Base
. . if (d>2) then
. . . m:=arg[3];
. . . #3rd param is desired Magnitude of Reverse number
. . fi;
. else
. . b:=10;
. . #default 2nd parameter to Base Ten
. fi;
. b:=AbsInt(b);
. #Base must be a positive number
. if (n>0) then
. . f:=1;
. . #positive-number flag
. else
. . f:=-1;
. . #negative-number flag
. . n:=-n;
. . #convert (n) to positive while working on it
. fi;
. d:=LogInt(n,b)+1;
. #get the Magnitude of (n) in Base (b)
. if (m<d) then
. #If no Magnitude parameter, or if it is invalid,
. . m:=d;
. . # Reverse-number's default Magnitude equals that of (n)
. fi;
. if (b<2) or (m=1) then
. . return(n*f);
. . #ignore Base Zero or Base One or a 1-digit number
. fi;
. r:=0;
. #initialize reverse-digits number
. repeat
. . d:=n mod b;
. . #get the last digit of (n)
. . n:=QuoInt(n,b);
. . #shrink (n)
. . r:=(r*b)+d;
. . #build up (r)
. . m:=m-1;
. . #reduce the Magnitude-counter as result is built
. until (n=0);
. return((b^m)*r*f);
. #apply any remaining Magnitude, and sign-flag --done!
end;

#In theory, could work with Base One, but it would be a
# waste of time. For example, in Base One the quantity
# "six" is written as 111111 --the reverse of any Base
# One number is always the same number. So, by not
# doing anything to (n) when (b=1), the function is
# actually correctly handling that case.

#NOTE: this function is not commutative by itself. For
# example, in Base Ten the reverse of 130 is 31, but the
# reverse of 31 is certainly not 130. However, the
# Magnitude parameter can allow the knowledgable user
# to obtain a degree of commutativity. Just specify
# 31,10,3 as the parameters to get 130 as the reverse
# of 31 (the user should know that "3" is the correct
# Magnitude value if previously had put 130 through this
# function).
-- Vernon, Dec 17 2010

about GAP http://www.gap-system.org/
As mentioned in the main text [Vernon, Dec 17 2010]

GAP language reference http://www.gap-syst...tm/ref/chapters.htm
for anyone who wants to know more [Vernon, Dec 17 2010]

!ydnah s'taht ,lleW
-- MaxwellBuchanan, Dec 17 2010


?aedi na siht si woH
-- pocmloc, Dec 17 2010


[pocmloc], the basic idea here was to do something using numerical techniques only, that typically is done via string-manipulation, but the "Magnitude" aspect of this function (see last paragraph) is new, I think.
-- Vernon, Dec 17 2010



random, halfbakery