Mathematica Journal: One Liners
- To: mathgroup at yoda.physics.unc.edu
- Subject: Mathematica Journal: One Liners
- From: ilan at leland.stanford.edu (ilan vardi)
- Date: Mon, 27 Apr 92 00:47:31 -0700
- Cc: wri-tech
In the new issue of the Mathematica Journal, One Liners, there is a
program called DigitalRoots which gives the result of iterating the
sum of the digits of an integer.
The code given there was
DigitalRoots[n_]:= FixedPoint[(Plus @@ IntegerDigits[#])&, n]
I have discovered the following more efficient program to compute this:
MyDigitalRoots[n_]:= Mod[n, 9]
There was also a program to reverse the digits of a number:
ReverseInteger[n_]:=
Module[{m = IntegerDigits[n], i},
Table[10^i, {i, 0,Length[m]-1}], m]
My program (which only takes One Line) is:
MyReverseInteger[n_]:= Fold[#1 10 + #2&, 0, Reverse[IntegerDigits[n]]]
-Ilan Vardi