Print 100 digits of π

Your challenge is to print any 100 consecutive digits of π. You must give the index at which that subsequence appears. The 3 is not included.

For example, you could print any of the following:

• 1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 (index 0)
• 8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196 (index 100)
• 4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273 (index 200)

Output may be as a string / digit list / etc.

To ensure answers actually produce known digits of pi, all answers must be verifiable through the Pi API (note: This API counts the 3, your answer should not).

This is , shortest wins!

• Does we need to actually run our code to completion, or can it be really slow?
– xnor
Oct 1, 2023 at 2:42
• But theoretically any sequence of integers eventually occurs in pi at some point... Oct 1, 2023 at 6:03
• @ShiranYuan But that would probably require calculating a googol digits of π to find out where that sequence starts.
– Neil
Oct 1, 2023 at 6:35
• Here are some experiments on the compression of pi digits (in binary format) by a fellow Intellivision game developer. TLDR: using standard compression algorithms is pointless. Oct 1, 2023 at 18:56
• @ShiranYuan Theoretically it is still unknown if pi is normal :) (doi.org/10.1080/10586458.2002.10504704) Oct 2, 2023 at 1:07

JavaScript (ES11), 53 bytes

Starts at index 0. Returns a BigInt.

f=(x=375n*10n**97n,k=3n)=>x?x/k+f(x*k++/k++/4n,k):82n


Try it online!

The underlying formula is:

$$\pi-3=\sum_{n=1}^{\infty}\frac{3}{4^n}\left(\prod_{k=1}^{n}\frac{2k-1}{2k}\right)\times\frac{1}{2n+1}$$

(also used in my answer to this other challenge)

This is mostly for answers that use a built-in arbitrary-precision pi constant, or a built-in that generates digits of pi.

Python 3, 49 bytes

from mpmath import*
mp.dps=102
print(str(pi)[3:])


Try it online!

05AB1E, 5 bytes

žs¦т£


Try it online!

VyxalH, 18 bitsv2, 2.25 bytes

ɾ∆i


Try it Online!

Explained

ɾ∆i­⁡​‎  # ‎⁡The H flag presets the stack to 100
ɾ    # ‎⁢For each number in the range [1, 100]:
∆i  # ‎⁣Push the nth digit of pi, 0 indexed, hence avoiding the 3
💎


Created with the help of Luminespire.

M, 12 bytes

ØP%1µ×1ȷ100Ḟ


Try it online!

Wolfram Language (Mathematica), 31 bytes

Print@@@RealDigits[Pi-3,10,100]


Try it online!

Charcoal, 13 bytes

Ｐ-×φψ¤≕PiＴ¹⁰⁰


Try it online! Link is to verbose version of code. Starts at the 997th digit of π. Explanation:

Ｐ-×φψ


Make space for 1999 characters in the canvas.

¤≕Pi


Fill those characters with the digits of π, including the leading 3., so 1997 decimals.

Ｔ¹⁰⁰


Take only the 100 digits starting at the 997th.

Ruby, 53 bytes

BigMath.PI guarantees accuracy for a number of digits up to its argument, but it outputs more digits than that (requiring truncation), and it just so happens that BigMath.PI(99) is still accurate enough for the problem, saving a byte over BigMath.PI(101).

require'bigdecimal/math'
p BigMath.PI(99).to_s[3,100]


Attempt This Online!

• People, don't upvote the CW, bringing it up on the vote list makes much cooler answers get less attention Oct 1, 2023 at 13:50
• I would note that mpmath is an external python library rather than in the standard libs
– Jo King
Oct 5, 2023 at 22:54

Charcoal, 32 bytes

≔⊗Ｘχ¹⁰⁰θ≔θηＦφ≔⁺θ÷×η⁻φι⊕⊗⁻φιη✂Ｉη¹


Try it online! Link is to verbose version of code. Explanation: Calculates the first 1001 terms of 2 + 1/3*(2 + 2/5*(2 + 3/7*(2 + 4/9*(2 + ...)))).

≔⊗Ｘχ¹⁰⁰θ


Calculate 2e100 as an integer.

≔θη


Start with that as an approximation to 1e100π.

Ｆφ


Repeat 1000 times.

≔⁺θ÷×η⁻φι⊕⊗⁻φιη


Calculate the next approximation to π.

✂Ｉη¹


Output without the leading 3.

• I tried other formulae but they were usually longer and would also have needed to compute extra digits or have a correction value added. Also for comparison string compression results in a 45 byte program.
– Neil
Oct 1, 2023 at 16:08

Python2, 91 bytes

print 0x296e417278c91bbebb472cab663a71405eef415a7751d7b251874642e34fa5406ba944ce62eb559eff7


Prints the first 100 digits starting at index 0.

• Sadly, noting that 0x296e417278c91bbebb472cab663a71405eef415a7751d7b251874642e34fa5406ba944ce62eb559eff7 == 9*117127*3643891*0xbdbf77d0b437e74747cd86c65905fc322c302b55f1756ff0169afdbfcebffea23ca51e5 is not helpful, because if c==a*b then c never has more digits than a*b
– Stef
Oct 3, 2023 at 13:43
• I just checked 51197 digits of pi to see if any indexes other than 0 would be shorter, and they weren't. Clever and well done, even if it won't win a prize. Oct 5, 2023 at 1:22
• @MarkRansom It could be possible to save bytes if there was a perfect square or power somewhere, ie writing 50digitnumber**2+smallremainder instead of 100digitnumber. But if the remainder also has 50 digits then it's useless.
– Stef
Oct 5, 2023 at 11:35
• Unfortunately I couldn't find a sequence that was even close to a perfect square. The best was at index 8328 but it would require 94 characters after adding the remainder. Oct 5, 2023 at 11:51
• I was thinking about what it would take to find a perfect square. Every 50-digit number can be squared to create a 100-digit number. But that means your chances are 1 in 10^50. Even for a million tries your odds are still only 1 in 10^44. Your odds of winning the Powerball jackpot 5 times in a row are higher. Oct 6, 2023 at 1:58

Octave, 24 bytes

Prints 100 digits of pi, starting at index 2

char(vpa(pi,102))(4:end)


Try it online!

MATLAB, 30 28 bytes

t=char(vpa(pi,102));t(4:end)


For both versions, this first uses variable precision calculation to work out Pi exactly to 102 digits (not decimals, it includes the 3).

Then it converts the result to a character vector and removes the string prefix '3.1' to leave exactly 100 characters. For the Octave version we can index directly into the returned value from the char() function, while MATLAB we have to save to a variable then truncate.

ans =
4159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798


Notes:

• We have to use 102 instead of 101 (3 + 100 decimals), because the vpa function rounds the final digit. If we used 101 the final digit would be incorrect due to rounding.
• Based on this meta post, when output is allowed to return a string, the ans= is acceptable.
• @LuisMendo Ah yea, I'm used to wrapping in @() to avoid the ans= hence my mind was programmed to avoid the array access. In this case we are required to return a string (rather than STDOUT), which means the ans= appears to be allowed. Oct 2, 2023 at 10:58

Kamilalisp, 21 bytes

↓ 2\⍫← \○⊢*(fr 101)\π


Attempt This Online!

Python 3, 165 bytes

(index 0)

s=''
a=10000
e=0
f=[a//5]*365
for c in range(364,0,-14):
d=0
for b in range(c,0,-1):
d=d*b+f[b]*a;g=2*b-1;f[b]=d%g;d//=g
s+=f"{e+d//a:04}";e=d%a
print(s[1:101])


Try it in SageMathCell (with verification).

This algorithm only uses integer arithmetic. It was derived from obfuscated C code by Dik T. Winter, but I've modified it a bit over the years. Dik's version was a bit more compact, and can generate hundreds of digits, but it eventually produces erroneous output due to integer overflow, which isn't an issue in Python. OTOH, this algorithm gets a bit sluggish for large numbers of digits, due to the O(n^2) inner loop.

Of course, for serious pi digit generation, there are various lovely AGM based algorithms that converge quadratically. The Borwein brothers found several, but I like the Gauss / Brent / Salamin https://stackoverflow.com/a/26478803/4014959

Java 10, 176 171 bytes

v->{var t=java.math.BigInteger.TEN.pow(100).shiftLeft(1);var p=t;for(int i=330;i>0;)p=t.add(p.multiply(t.valueOf(i)).divide(t.valueOf(i-~i--)));return(p+"").substring(1);}


Based on my answer for the related challenge of outputting the first 500 digits of pi.
-5 bytes thanks to @Neil by reverting the algorithm

Try it online.

• Calculating in reverse increases accuracy allowing you to save bytes: Try it online!
– Neil
Oct 4, 2023 at 21:21

05AB1E, 45 44 bytes

8₂;×•13CÒ₃Sˆ£û‰ñêˆǝd‰Ž¿Ð†₆{©«62—lÚÌ”ß°dZγ•J


Try it online! Starts at index $$\2{,}164{,}164{,}669{,}331\$$.

• This is nice, and (I think) the first answer to actually exploit 'compression' by choice of position. It would be 40.5 bytes by porting to Nibbles, but you deserve the credit for this & I won't post it separately... Oct 10, 2023 at 8:26

Vyxal, 70 bitsv2, 8.75 bytes

u∆C₁⇧ḞḢḢṪ


Try it Online!

Calculates pi through $$\cos^{-1}(-1)\$$ and then formats to 100 decimal places.

Explained

u∆C₁⇧ḞḢḢṪ­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌­
u∆C        # ‎⁡arccos(-1)
₁⇧Ḟ     # ‎⁢Formatted to 102 decimal places
ḢḢ   # ‎⁣Remove the 3.
Ṫ  # ‎⁤and the last digit. The last digit is needed for rounding purposes.
💎


Created with the help of Luminespire.

Python 3, 256 bytes

This version computes pi using an accelerated version of Archimedes' algorithm for pi = 4*arctan(1).

m=338
M=1<<m
def s(y,x=M):
y<<=m;u=x
while u<0 or u>1:u=(x//2+y)//x-x;x+=u>>1
return x
a=b=x=M
g=s(2*M)
d=[a]
for i in range(16):
a=(a+g)>>1;p,d=d,[a];q=0
for u in p:q+=2;d+=[d[-1]-(u>>q)]
b-=b>>q;g=s(a*g>>m,g)
print(str(b*x//d[-1]*4*10**100>>m)[1:])


Try it in SageMathCell

This code implements binary fixed point arithmetic using Python integers. The core arctan algorithm was devised by Bille Carlson, see Carlson, B. C. An Algorithm for Computing Logarithms and Arctangents, Mathematics of Computation, Apr 1972.

This algorithm rapidly calculates any of the standard inverse circular and hyperbolic functions. It even handles complex arguments (assuming the sqrt function does). Here's a non-golfed demo that computes pi = 10*arcsin((sqrt(5)-1)/4).

This algorithm is very similar to the AGM (arithmetic-geometric mean), but it updates the two core terms a and g` sequentially instead of in parallel. So it's sometimes referred to as the fake AGM. It's also known as Borchardt's modified AGM algorithm. The Borwein brothers pointed out that it is, in fact, the same algorithm Archimedes used to iteratively subdivide a polygon.

Carlson's innovation is to apply Richardson series acceleration, which speeds up the convergence considerably. It's not as fast as a true AGM, however it's quite efficient for high precision calculation because it only uses one non-trivial division. (The square root can be also be implemented to avoid non-trivial divisions, using the Quake-style 1/sqrt algorithm).

For further details on algorithms related to pi and the AGM, please see The Borwein brothers, Pi and the AGM by Richard P. Brent.

FWIW, Carlson also developed and analysed a suite of true AGM algorithms for computing elliptic integrals and functions, and those algorithms can (of course) handle circular & hyperbolic functions. See Numerical computation of real or complex elliptic integrals