15
$\begingroup$

I happened to watch a Youtube video on Pi. According to the video, the 1 millionth digit of Pi is 1. And here is another page of the first 1 million digits of Pi.

You can get the same answer from WolframAlpha.

However, if you let Mathematica to calculate the digits you will get:

N[Pi, 1000005]
....65200102821**3**0222`1000005.

You will notice the 1 millionth digit of Pi in Mathematica is 3, not 1. Is anything wrong here?

Actually, if you move back 32 digits, you can see the exact digits as in the video.

**5779458151**309275628320845315846520010282130222`1000005.

Updates:

According to the answers, you can obtain the correct digits by following commands:

  1. Command from answer of @m_goldberg

    RealDigits[N[Pi, 1000001]][[1, -10 ;; -1]]

  2. Similarly, you can convert the number to string, which is the answer of @Daniel_Lichtblau

    str = ToString[N[Pi, 1000001], InputForm];
    Characters[str][[1000002 - 9 ;; 1000002]]

  3. RealDigits can extract specific digits as in the answer of @Mr.Wizard

    RealDigits[Pi, 10, 10, 9 - 1*^6]

The last one is much faster than others:

In[334]:= Timing[RealDigits[Pi, 10, 10, 9 - 1*^6]]

Out[334]= {0.036622, {{5, 7, 7, 9, 4, 5, 8, 1, 5, 1}, -999990}}

In[335]:= Timing[RealDigits[N[Pi, 1000001]][[1, -10 ;; -1]]]

Out[335]= {0.229211, {5, 7, 7, 9, 4, 5, 8, 1, 5, 1}}

If this problem is generalized to obtain 1 million digits after decimal mark, the first two commands may provide wrong results. As is mentioned in the answer of @Mr.Wizard, the result provided by RealDigits[Pi, 10, 10, 9 - 1*^6] is the 999,991 to 1,000,000 digits behind decimal mark, nevertheless how many digits before the decimal mark. But for the first two method these digits should be counted and subtracted from the result. For the second method, the decimal mark takes one character, which should be included in the calculation.

The first method can be modified to following codes to consider the digits before decimal mark, but enough digits must be obtained in the first command:

num = RealDigits @ N[Pi, 1000010];   
num[[1,999991+num[[2]];;1000000+num[[2]]]]

Conclusions

  1. Command N does not guarantee to output exactly number of digits as the command requiring. Moreover, different results may be obtained in different version of Mathematica.

  2. RealDigits with four arguments is the most efficient way to extract specific digits in a number.

  3. Converting number to InputForm is another possible way to obtain the digits without RealDigits command.

$\endgroup$
  • $\begingroup$ Maybe my Mathematica is broken. I get ...5779458151309275628320845315846520010277973264`1000005. from N[Pi, 1000005]. But everybody else seems to get what you get. :/ $\endgroup$ – Michael E2 May 4 '15 at 15:31
  • $\begingroup$ @MichaelE2 I actually obtained the same result as Michael E2! I am on Mathematica v. 10.1.0 on Win7-64bit. $\endgroup$ – MarcoB May 4 '15 at 15:50
  • 3
    $\begingroup$ I really think this is about confusing the 1,000,000 digit of π with the 1,000,000 decimal place of π. $\endgroup$ – m_goldberg May 4 '15 at 16:24
  • 5
    $\begingroup$ I don't think this question should be closed. The OP has made a mistake, but it isn't a simple one nor is easy to find the needed info to correct it in the docs. $\endgroup$ – m_goldberg May 4 '15 at 16:59
  • 1
    $\begingroup$ @MarcoB I get kattern's result on V9.0.1. Must've been a change in the algorithm. (Our result agrees with Daniel Lichtblau's. See below.) $\endgroup$ – Michael E2 May 4 '15 at 18:38
35
$\begingroup$

You have selected the wrong digit. Mathematica gets the digit in the million-th decimal place right if the calculation is performed correctly.

 q = N[Pi, 1000010];
 RealDigits[q][[1, 1000001]]
1

I take the 1000001-th digit because RealDigits includes the integer part, 3.

Update

It is really important to use RealDigits to decide this question. Looking at the displayed full form number is not reliable because it shows extra digits added in the working precision needed to get the specified real precision. Consider

N[Pi, 20] // FullForm
  3.1415926535897932384626433832795028841971693993751058209749`20.

That's a lot more than the 20 digits asked for. However,

RealDigits @ N[Pi, 20]
{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4}, 1}

gives the actual set of correct digits.

$\endgroup$
  • $\begingroup$ Why can not I directly read the digits from the result? Does it mean Mathematica print a close but unknown digits out? $\endgroup$ – Kattern May 4 '15 at 13:43
  • $\begingroup$ @kattern, your count is off by 1. Look at the digit before the 3 you highlighted. $\endgroup$ – J. M. will be back soon May 4 '15 at 13:54
  • $\begingroup$ @kattern. Given ...6520010282130222`1000005 and counting backwards from 1000005, 1 is the digit that appears in the 1000000-th decimal place. You need to count down 5, 4, 3, 2, 1, 0. $\endgroup$ – m_goldberg May 4 '15 at 13:55
  • 1
    $\begingroup$ @kattern. NO, not when going backwards. The precision value 1000005 refers the number of decimals places computed. $\endgroup$ – m_goldberg May 4 '15 at 13:58
  • 1
    $\begingroup$ the precision value to N also includes the integer part. `StringTake[ ToString[N[Pi, 1000001]] , {-1}] -> (*1*) . (add one for the decimal and note no roundoff issue here since the next digit is a 3.. ) $\endgroup$ – george2079 May 4 '15 at 15:58
16
$\begingroup$

That N[Pi, 1000005] is simply showing guard digits as well as the certified digits. You can get some idea of this from the checks below.

npi = N[Pi, 10^6 + 5];
digits = RealDigits[npi];
digits[[1, -5 ;; -1]]
str = ToString[npi, InputForm];
StringLength[str]
Characters[str][[-(StringLength[str] - 10^6 - 1) ;; -1]]

(* Out[426]= {1, 3, 0, 9, 2}

Out[428]= 1000047

Out[429]= {"1", "3", "0", "9", "2", "7", "5", "6", "2", "8", "3", \
"2", "0", "8", "4", "5", "3", "1", "5", "8", "4", "6", "5", "2", "0", \
"0", "1", "0", "2", "7", "7", "9", "7", "3", "2", "6", "4", "`", "1", \
"0", "0", "0", "0", "0", "5", "."} *)
$\endgroup$
13
$\begingroup$

To complement the existing answers I would like to point out that these digits can be efficiently produced directly by RealDigits without the use of N etc.:

RealDigits[Pi, 10, 10, 9 - 1*^6]
{{5, 7, 7, 9, 4, 5, 8, 1, 5, 1}, -999990}

Regarding the updated question I believe you are still confused by the different index between the different methods. Please consider this example:

x = 5000 + 1/7;

ToString[N[x, 10]]
RealDigits[N[x, 10]]
RealDigits[x, 10, 10]
RealDigits[x, 10, 10, -1]
"5000.142857"

{{5, 0, 0, 0, 1, 4, 2, 8, 5, 7}, 4}

{{5, 0, 0, 0, 1, 4, 2, 8, 5, 7}, 4}

{{1, 4, 2, 8, 5, 7, 1, 4, 2, 8}, 0}

Note that the first three methods produce digits starting with 5000, whereas the last method starts from the right of the decimal place due to the index chosen. So if you use the last method you must index from the decimal, whereas the other methods must all index from the "left" most digit.

$\endgroup$
  • $\begingroup$ Ha, maybe I am so stupid on this topic. RealDigits[Pi, 10, 10, 9 - 1*^6] and RealDigits[N[Pi, 1000001]][[1, -10 ;; -1]] will produce the same results, but the former one is faster. The silly thing is RealDigits[1/7, 10, 10, 9 - 1*^6] and RealDigits[N[1/7, 1000001]][[1, -10 ;; -1]] giving total different results. What is happening here? $\endgroup$ – Kattern May 5 '15 at 1:45
  • $\begingroup$ @kattern Stupid? I think not; this can be confusing. The output of the two expressions you give is not totally different on my system; they only vary by one place. As documented: "RealDigits[x, b, len, n] gives len digits starting with the coefficient of b^n" -- simply watch your offset. Note that in the output of RealDigits the second part shows the number of digits to the left of the decimal point, with negative values possible. $\endgroup$ – Mr.Wizard May 5 '15 at 2:12
  • $\begingroup$ Please check the updated question. If the difference is coming from the offset, the commands for calculating Pi should have the same offset. But, they do not. $\endgroup$ – Kattern May 5 '15 at 2:17
  • $\begingroup$ @kattern Please see the update. $\endgroup$ – Mr.Wizard May 5 '15 at 3:19
  • 1
    $\begingroup$ @kattern Without the fourth parameter RealDigits will return a list starting from the first significant digit. Consider RealDigits[1/7000, 10, 10] returning {{1, 4, 2, 8, 5, 7, 1, 4, 2, 8}, -3}. Sorry if I did not make this clear before. $\endgroup$ – Mr.Wizard May 5 '15 at 5:47

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.