| Bytes | Lang | Time | Link |
|---|---|---|---|
| 105 | Java 10 | 231002T092617Z | Kevin Cr |
| 022 | APLNARS | 250616T183315Z | Rosario |
| 086 | dc | 250615T115548Z | Toby Spe |
| 080 | Python2 | 231003T123405Z | Stef |
| nan | 231001T022808Z | emanresu | |
| 044 | 05AB1E | 231005T162437Z | Makonede |
| 256 | Python 3 | 231004T153907Z | PM 2Ring |
| 165 | Python 3 | 231004T034318Z | PM 2Ring |
| 024 | Octave | 231001T225442Z | Tom Carp |
| 021 | Kamilalisp | 231001T172812Z | Kamila S |
| 053 | JavaScript ES11 | 231001T072621Z | Arnauld |
| 032 | Charcoal | 231001T075517Z | Neil |
| 875 | Vyxal | 231001T023814Z | lyxal |
Java 10, 176 171 167 105 bytes
v->"1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
Java won't be able to beat a hard-coded approach..
-53 bytes thanks to @ceilingcat
Old 176 171 167 bytes answer:
v->{var t=java.math.BigInteger.TEN.pow(100);var p=t=t.add(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
-4 bytes thanks to @ceilingcat
APL(NARS), 22 chars
{⎕fpc←!6⋄¯1↓3↓101⍕○1v}
It would print the first 100 digits starting at index 0.
○1 is Pi in APL float, ○1v is Pi in float format controlled from ⎕fpc, that is the lenght in bit of
the numbers.
It would use float of !6 or 720 binary digits (it seems enough because 3.322x100<720,
numbers of 720 bits) than one game on string rapresentation of Pi returned from ⍕,
cut the first 3 chars and the last.
{⎕fpc←!6⋄¯1↓3↓101⍕○1v}
1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
dc, 86 bytes
16i3EA343 F^D0AF21269849B78C58FC333F941E6012ED53642AA920ECF5ACED1D757BC22EF4B01B84D3+p
This prints the 100 digits starting at position 107805.
How it works
Using hexadecimal input (the largest single-char base supported by dc), we simply evaluate the arithmetic expression 4105027¹⁵ + 405404108576547646222286831925479674828335033021104647365110581965912463389531299349715.
The more interesting part of this answer is the Python script I used to search for this expression, although it's admittedly unpolished and under-optimised. Starting with pi set to a string containing as many digits of π as you wish to search,
def find(x, recurse=3):
return min(decompose(x, recurse), key=len)
def decompose(x, recurse):
yield f"{x:X}"
if not recurse:
return
for e in range(5,100):
root = int(x ** (1/e) + .5)
if root < 2:
return
mod = x - root**e
if len(f"{root:X}{e:X}{mod:+X}") > len(f"{x:x}++"):
continue
sign = f"{mod:+}"[0]
yield f"{root:X} {e:X}^{find(abs(mod), recurse-1)}{sign}"
for n in range(len(pi)-100):
if n % 10000 == 0:
print(f"\r{n=:06}", end='', flush=True)
if pi[n] == '0':
continue
p = int(pi[n:n+100])
s = f"16i{find(p)}p"
if len(s) < 87:
print(f"\r{n=}: {s} # {len(s)} bytes")
print('')
Python2, 80 bytes
print int('cpe033khbca8dn1axx5co9dg2uqpcff5s7pgqieoqv1o345ekaffzcw4clbpywvr',36)
Prints the first 100 digits starting at index 0.
Previous answer was 91 bytes in base-16 as 0x..., but squareroot12621 suggested using base-36 as int(...,36).
Trivial answers
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:])
05AB1E, 5 bytes
žs¦т£
Vyxal H, 18 bitsv2, 2.25 bytes
ɾ∆i
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Ḟ
Wolfram Language (Mathematica), 31 bytes
Print@@@RealDigits[Pi-3,10,100]
Charcoal, 13 bytes
P-×φψ¤≕PiT¹⁰⁰
Try it online! Link is to verbose version of code. Starts at the 997th digit of π. Explanation:
P-×φψ
Make space for 1999 characters in the canvas.
¤≕Pi
Fill those characters with the digits of π, including the leading 3., so 1997 decimals.
T¹⁰⁰
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]
05AB1E, 45 44 bytes
8₂;ו13CÒ₃Sˆ`£û‰ñêˆǝd‰Ž¿Ð†₆{©«62—lÚ̔߰dZγ•J
Try it online! Starts at index \$2{,}164{,}164{,}669{,}331\$.
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
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
Octave, 24 bytes
Prints 100 digits of pi, starting at index 2
char(vpa(pi,102))(4:end)
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
vpafunction 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.
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
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)
Charcoal, 32 bytes
≔⊗Xχ¹⁰⁰θ≔θηFφ≔⁺θ÷×η⁻φι⊕⊗⁻φιη✂Iη¹
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 + ...)))).
≔⊗Xχ¹⁰⁰θ
Calculate 2e100 as an integer.
≔θη
Start with that as an approximation to 1e100π.
Fφ
Repeat 1000 times.
≔⁺θ÷×η⁻φι⊕⊗⁻φιη
Calculate the next approximation to π.
✂Iη¹
Output without the leading 3.
Vyxal, 70 bitsv2, 8.75 bytes
u∆C₁⇧ḞḢḢṪ
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.