Does Mathematica get Pi wrong?

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**02221000005.

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**3092756283208453158465200102821302221000005.

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:= Timing[RealDigits[Pi, 10, 10, 9 - 1*^6]]

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

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

Out= {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[];;1000000+num[]]]

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.

• Maybe my Mathematica is broken. I get ...57794581513092756283208453158465200102779732641000005. from N[Pi, 1000005]. But everybody else seems to get what you get. :/ – Michael E2 May 4 '15 at 15:31
• @MichaelE2 I actually obtained the same result as Michael E2! I am on Mathematica v. 10.1.0 on Win7-64bit. – MarcoB May 4 '15 at 15:50
• I really think this is about confusing the 1,000,000 digit of π with the 1,000,000 decimal place of π. – m_goldberg May 4 '15 at 16:24
• 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. – m_goldberg May 4 '15 at 16:59
• @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.) – Michael E2 May 4 '15 at 18:38

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.141592653589793238462643383279502884197169399375105820974920.

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.

• Why can not I directly read the digits from the result? Does it mean Mathematica print a close but unknown digits out? – Kattern May 4 '15 at 13:43
• @kattern, your count is off by 1. Look at the digit before the 3 you highlighted. – J. M. will be back soon May 4 '15 at 13:54
• @kattern. Given ...65200102821302221000005 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. – m_goldberg May 4 '15 at 13:55
• @kattern. NO, not when going backwards. The precision value 1000005 refers the number of decimals places computed. – m_goldberg May 4 '15 at 13:58
• 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.. ) – george2079 May 4 '15 at 15:58

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= {1, 3, 0, 9, 2}

Out= 1000047

Out= {"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", "."} *)

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.

• 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? – Kattern May 5 '15 at 1:45
• @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. – Mr.Wizard May 5 '15 at 2:12
• 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. – Kattern May 5 '15 at 2:17
• @kattern Please see the update. – Mr.Wizard May 5 '15 at 3:19
• @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. – Mr.Wizard May 5 '15 at 5:47