6
$\begingroup$

I want to display a rational number in Mathematica in periodic style. PeriodicForm isn't working anymore. It worked in Mathematica 5 and now I'm using Mathematica 9.

I want to display the number $3.13678989898989898989\ldots$, where the repeating $89$ part should be displayed as $3.1367\overline{89}$

$\endgroup$
  • 2
    $\begingroup$ See this is RealDigits the replacement for what you want? $\endgroup$ – bobthechemist Oct 29 '13 at 13:54
  • $\begingroup$ i want to display the following number 3.13678989898989898989 and the 8989 part should be displayed as periodic $\endgroup$ – user2147674 Oct 29 '13 at 13:58
  • $\begingroup$ Your question implies that there is/used to be some kind of solution or function (PeriodicForm?) which does not work anymore, which is a bit puzzling. $\endgroup$ – Yves Klett Oct 29 '13 at 14:03
  • $\begingroup$ books.google.at/… $\endgroup$ – user2147674 Oct 29 '13 at 14:04
  • 1
    $\begingroup$ @YvesKlett Apparently it was a function in the NumberTheory package that was in versions 6 and earlier. $\endgroup$ – bobthechemist Oct 29 '13 at 14:05
13
$\begingroup$

You can always write your own version of PeriodicForm:

ClearAll@PeriodicForm
PeriodicForm[n_] := RealDigits[n] /. {{d___Integer, {i__Integer} ...}, l_Integer} :> 
    PeriodicForm[n, l, {d}, {i}]

Format[PeriodicForm[n_, l_, d_, i_]] ^:= Interpretation[Row[{
    FromDigits[d ~Take~ l] /. {} -> 0,
     ".", 
    Sequence @@ d ~Drop~ l, 
    OverBar[FromDigits@i]
}] /. OverBar[0] :> Sequence[], n]

enter image description here

$\endgroup$
  • $\begingroup$ Nice - I started down this route, then realized I was just procrastinating from all the grading I have to do. $\endgroup$ – bobthechemist Oct 29 '13 at 16:12
8
$\begingroup$

To get PeriodicForm working in Mathematica 9 (and probably other versions after 6) you need to first download the obsolete package from the Wolfram Library Archive. Run the package, ignore the errors and have fun:

Get["http://library.wolfram.com/infocenter/MathSource/6773/\
   ContinuedFractions.m?file_id=6182"]
PeriodicForm[RealDigits[19/7]]

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ You could use URLSave to install package. URLSave["library.wolfram.com/infocenter/MathSource/6773/…", FileNameJoin[{$UserBaseDirectory, "Applications", "ContinuedFractions.m"}]] and << ContinuedFractions` $\endgroup$ – halmir Oct 29 '13 at 15:03
  • $\begingroup$ I wonder why this package is not integrated into the kernel. I suspect there are some subtleties that it cannot yet handle. Btw, you may want to show the FullForm of your example. $\endgroup$ – DavidC Oct 29 '13 at 15:48
5
$\begingroup$

Or, without the package, you can use

RealDigits[19/7]
{{2, {7, 1, 4, 2, 8, 5}}, 1}

which shows the repeated decimal portion in the second (list) element of the answer. This tells you that the answer is 2 followed by repeating 714285. The final 1 is the exponent and allows the same representation to handle much larger or smaller numbers.

$\endgroup$
  • $\begingroup$ Yes, RealDigits is the place to start. $\endgroup$ – DavidC Oct 29 '13 at 15:22
4
$\begingroup$

Here you will find a discussion of issues related to the production of repeated decimals.

The code is reproduced below for convenience.

repeatingDecimal[n_Integer | n_Real] := n

Format[repeatingDecimal[q_Rational]] := 
Row@Flatten[{IntegerPart@q, ".", RealDigits@FractionalPart@q} /. {{nr___Integer, r_List: {}}, 
  pt_} :> {Table[0, {-pt}], nr, OverBar /@ r}]

repeatingDecimal[q_] + x_ ^:= q + x
repeatingDecimal[q_]*x_ ^:= q*x
repeatingDecimal[q_]^x_ ^:= q^x

Examples

n1 = 1; n2 = 15; ClearAll[i, k, r];
TableForm[Table[repeatingDecimal[i/j], {i, n1, n2}, {j, n1, n2}], 
TableHeadings -> {None, Table[("r")/k, {k, n1, n2}]}]

table


Simple arithmetic operations such as addition can be carried out on the repeating decimals.

a = repeatingDecimal[7/31];
b = repeatingDecimal[24/31];
Print["a = ", a]
Print["b = ", b]
Print["a + b = ", a, " + ", b, " = ", a + b]

notation

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.