M (MUMPS) - 7272 70
R n F i=0:1:n W !,i S r=$TR($RE(i),69,96) I '$TRW:r=+r*r'=i*'$TR(i,0689),i'=r,r=+r W "."
Most built-in commands and functions in M have abbreviated versions. I've used the full names below.
READ n
- Read a string from the keyboard and store it in n
.
FOR i=0:1:n
- Loop from zero to n
, incrementing i
by 1 each time. (The remainder of the line constitutes the body of the loop.)
WRITE !,i
- Print a newline followed by the value of i
.
SET r=$TRANSLATE($REVERSE(i),69,96))
- Reverse i
, replace nines with sixes and sixes with nines, and store that in r
IF
- Exactly what it sounds like (multiple comma-separated arguments are ANDed together).
'$TRANSLATEWRITE:r=+r*r'=i*'$TRANSLATE(i,0689)
- Remove all zeros, sixes, eights, and nines from i
, and see if there's anything left (' "."
is the logical negation operator)
i'=r
- Check that i
isn't the same as it's flipped version r
:
- Denotes a postconditional expression, so theWRITE
command is only executed ifr=+r*r'=i*'$TRANSLATE(i,0689)
evaluates to a truthy value.r=+r
- Check thatr
doesn't have a leading zero. The unary+
operator converts a string to a number, which strips leading zeroes if there are any.*
- Multiplication operator. M has no order of operations; all binary operators are evaluated in the order they appear from left to right.r'=i
- Check thati
isn't the same as it's flipped versionr
.'$TRANSLATE(i,0689)
- Remove all zeros, sixes, eights, and nines fromi
, and check that there's nothing left. ('
is the logical negation operator.)"."
- Finally the argument to theWRITE
command (a literal string).
r=+r
- Check that r
doesn't haveEdit: Made it a leading zero. The unary +
little shorter by abusing the multiplication operator converts a string to a number, which strips leading zeroes if there are any. Previous version:
WRITE "."
- Print a dot.
R n F i=0:1:n W !,i S r=$TR($RE(i),69,96) I '$TR(i,0689),i'=r,r=+r W "."