Find the 5566th digit after the decimal point of 7/101



I want to find the 5566th digit after the decimal point of 7/101. I input the following code into Mathematica 11:

Mod[IntegerPart[7/101*10^5566], 10]

The output is 6, which is the correct answer. Is there a better way to find the answer? Thank you very much in advance.

Wei-Cheng Liu

Posted 2016-08-29T01:05:28.633

Reputation: 409

8The best way to find this digit is in my opinion to calculate it. Since 7/101 ist periodic and so every digit in position n for which mod(n,4)=2 is 6. And mod(5566,4)=2. – mgamer – 2016-08-29T04:34:37.370

1@mgamer great point. Intelligence wins over brute force. – Mr.Wizard – 2016-08-29T04:37:09.523

2@Mr.Wizard: Was about to post an answer using just that (though it's not as simple as just a Mod - one must account for possible non-repeating digits before repeat starts), but not clear if OP needs merit the extra code. It will be orders of magnitude faster when going out millions+ digits, but for less, probably no real advantage over your answer... – ciao – 2016-08-29T04:48:00.813



Fast algorithm

n = 5566
IntegerPart[10 Mod[7 PowerMod[10, n - 1, 101], 101]/101]

A brute force approach (see also these posts on stackoverflow :) ) may be fine for the current problem, but what if n is a huge number? The only possibility apart from guessing the periodic sequence of numbers as mgamer suggested would be to use modular arithmetics. Let me explain my answer. In contrast to the original post we put the number of interest not in the last digit of the integer part, but in the first digit of the fractional part. Conveniently, the fractional part can be computed as a reminder, or for higher efficiency by PowerMod.

Let us compare the timing of the two methods:

n = 556612345;
Mod[IntegerPart[7 10^n/101], 10] // Timing
(*{10.447660, 3}*)
IntegerPart[10 Mod[7 PowerMod[10, n - 1, 101], 101]/101] // Timing
(*{0.000016, 3}*)

The time difference is obvious!


Let us consider another example, we compute the n=6 digit of the 7/121 fraction.

n = 6
N[7/121, 30]


In the original post the sought digit is the last digit of the integer part:

N[7 10^n/121, 20]


whereas in my solution it is the first digit in the fractional part

N[Mod[7*10^(n - 1), 121]/121, 20]

0.12396694214876033058 .

It is further used that Mod[a 10^b,c]=Mod[a PowerMod[10,b,c],c].

Reusable function

As requested in the comments, a reusable function can be provided:

nthDigitFraction[numerator_Integer, denominator_Integer, n_Integer, 
   base_Integer: 10] /; n > 0 && base > 0 && denominator != 0 := 
 Module[{a = Abs[numerator], b = Abs[denominator]}, 
  IntegerPart[base Mod[a PowerMod[base, n - 1, b], b]/b]]


Posted 2016-08-29T01:05:28.633

Reputation: 11 476

2That's the way to do it. +1 – ciao – 2016-08-29T08:03:20.043

2@Mr.Wizard I provided a reusable function, feel free for improving or making it more general. – yarchik – 2016-08-29T09:09:19.967

1@yarchik if you looked at my code and read its introduction I DO NOT use brute force. In this case the recurring cycle is length 4 so determining digit is almost trivial. I applaud your method. Please note my function is general but not tweeted and used modular arithmetic. – ubpdqn – 2016-08-29T09:46:44.860

1@ubpdqn Thank you, I modified my post accordingly. However, if you try your method on larger numbers the computational time grows rather fast. For instance I could not have computed dec[7, 101345, 5566]with your method. – yarchik – 2016-08-29T10:17:03.123

2@yarchik yes and that is why I voted for your answer. It is very efficient and clean and was a nice lesson for me. The achilles heal of my approach was the need (not to process all the digits) but determing the cylce. :) – ubpdqn – 2016-08-29T10:19:43.593

Quick question: Is there a reason for N[Mod[7 *10^(n - 1), 121], 121]/121, 20] to have 2 left brackets and 3 right brackets? I'm having trouble understanding what's going on here. – Sherlock9 – 2016-09-26T13:22:06.213

@Sherlock9 Thank you for spotting a misprint. Now it is corrected. – yarchik – 2016-09-27T07:47:25.327


An alternative formulation of RealDigits that I prefer:

RealDigits[7/101, 10, 1, -5566][[1, 1]]

(* 6 *)

This yields better performance which becomes important when looking for deeper digits:

d = 6245268;

RealDigits[7/101, 10, 1, -d][[1, 1]]  // AbsoluteTiming
RealDigits[7/101, 10, d - 1][[1, -1]] // AbsoluteTiming
{0.0501477, 3}

{1.06702, 3}

Recommended reading:

For comparison to other methods now posted RealDigits can compute the repeating decimal itself:

{{{6, 9, 3, 0}}, -1}

How to programmatically work with this output was the subject of a different Question, though I cannot find it at the moment. The possible combinations of repeating and nonrepeating digits as well as the offset makes a truly elegant yet robust solution difficult (at least to me) but in the easiest case, which this happens to be:

d = 5566;

RealDigits[7/101] /.
  {{c_List}, o_} :>
    c[[ Mod[d + o, Length @ c, 1] ]]

(* 6 *)

This is of course quite fast:

d = 556612345;

RealDigits[7/101] /.
  {{c_List}, o_} :> c[[ Mod[d + o, Length@c, 1] ]] // RepeatedTiming
{0.00001243, 0}


Posted 2016-08-29T01:05:28.633

Reputation: 259 163


Use RealDigits

RealDigits[7/101, 10, 5566][[1]][[5565]]


Posted 2016-08-29T01:05:28.633

Reputation: 7 105

1FWIW this could also be written RealDigits[7/101, 10, 5566 - 1][[1, -1]]. +1 of course, but see the performance caveat in my answer. – Mr.Wizard – 2016-08-29T04:08:07.567



I thank yarchik for his answer and his test of my code identified an error (as well as my code being extremely inefficient for long cycle length):

For this particular example, the recurring patter is of length 4.


fun[n_, d_] := 
 Module[{lst = 
    NestWhileList[QuotientRemainder[10 #[[2]], d] &, 
     QuotientRemainder[n, d], UnsameQ, All], p}, 
  p = Position[lst, lst[[-1]]][[1, 1]];
  {lst[[1 ;; p - 1, 1]], lst[[p ;; -2, 1]]}]
dec[n_, d_, p_] := Module[{a, b},
  {a, b} = fun[n, d];
  b[[Mod[p-Length@a+1, Length@b, 1]]]]


  • fun just separates cycling and non-cycling.
  • dec determines value of at position p by working out where in cycle p is


dec[7, 101, 5566]
dec[7, 101, 6245268]

yields 6 and 3 respectively.

As a reality check (not proof):

{#, RealDigits[7/101, 10, 1, -#][[1, 1]], dec[7, 101, #]} & /@ 
  RandomInteger[{10000, 1000000}, 20] // TableForm

enter image description here


Posted 2016-08-29T01:05:28.633

Reputation: 53 491