If I have a list, say $\{\frac{1}{36},\frac{1}{36},\frac{1}{18},\frac{1}{18},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{12},\frac{1}{18},\frac{1}{18},\frac{1}{36},\frac{1}{36}\}$, how can I make it so that all the elements of the list have the same denominator (using the smallest one possible) ?
4 Answers
$\begingroup$
$\endgroup$
l = {1/36, 1/36, 1/18, 1/12, 1/18};
lcm = LCM @@ (Denominator@l);
ToString[#]/lcm & /@ (l lcm);
$\begingroup$
$\endgroup$
If it is only for printing you could do something like this
list = {1/36, 1/36, 1/18, 1/12, 1/18};
max = Max@Denominator@list;
p1 = ToString /@ max/Denominator@list;
p2 = Array[ToString@max &, Length@list;
Column[#, Center, Spacings -> 1, Dividers -> Center] & /@ Transpose[{p1, p2}]
$\begingroup$
$\endgroup$
2
Not only for printing. One more option is to use HoldForm[]
(or Defer[]
suggested by J.M.):
Clear[list, den, print];
list = {1/36, 1/36, 1/18, 1/12, 1/18};
HoldForm:
den = Denominator@list;
print = HoldForm /@ (Max[den]/den)/Max[den]
To evaluate the expression:
ReleaseHold@print
PS. One may olso use Inactivate[]
-
$\begingroup$ I would prefer
Defer[]
(orInactivate[]
in newer versions) myself. $\endgroup$ Commented Nov 26, 2015 at 22:56 -
$\begingroup$ @J.M. Why? Is it seriuos difference? I've just put it to avoid
ToString
solution. $\endgroup$– garejCommented Nov 27, 2015 at 6:20
$\begingroup$
$\endgroup$
Using FractionBox
:
For display only:
list = {1/36, 1/36, 1/18, 1/12, 1/18};
With [{z = LCM @@ Denominator@list},
FractionBox @@@
Transpose[{(list z), ConstantArray[z, Length@list]}] // DisplayForm
]
Inactive[Divide] @@@
Transpose[{(list z), ConstantArray[z, Length@list]}]
Activate[%]
printed
.Mathematica will simplify them on the fly. There are several related question to this on this site. Search for them $\endgroup$LCM@@Denominator[list]]
might be what you want. $\endgroup$