Subj : Re: Reversing a number To : comp.programming From : Gerry Quinn Date : Thu Sep 15 2005 11:33 am In article , willem@stack.nl says... > Gerry wrote: > ) Not directly, but conceptually it is all about manipulating the base-10 > ) string representation. What are easily comprehensible in these terms > ) turn into kludgy numeric operations instead. When I take x % 10, I am > ) determining a value that encodes a digit in the string representation. > ) The right end of the string, as I look at the string representing the > ) number. > > That may be true, but in the context of the statement that started this > whole discussion*, it is not relevant what the code is doing conceptually. > > *) The statement was that the algorithm 'convert to string, reverse string, > convert back to number' is more complex than the algorithm involving > direct manipulation of mumbers. I disagree, because while the problem was stated as 'reverse a number', it really means 'find a number corresponding to the reverse of the string representation of a number in base-10'. Conversion to a string is conceptially implicit in the problem, and this is in fact what is being done by the numeric calculations. 14 is not a number - the number represented by the strings 14 (base 10), 1110 (base 2) and 11111111111111 (unary) is a number. If the problem is to calculate '41' given 14 the only meaning of this problem is to convert this number to a base-10 string representation and reverse it, then convert back to a number. This is what your algorithm does, but in an obscure fashion. 14 % 10 gives 4, which is a number corresponding to the last character of the base-10 representation of the original, and the first character of the base-10 representation of the result. We could explicitly do the conversions, with a string reversal in the middle, using arithmetic. All your algorithm does is mix the three operations together, which does save some memory registers but is hardly simpler in any real sense. And, as can be seen, whether it requires fewer statements depends on the language used. - Gerry Quinn .