A Triangular Slice of Squared Pi
05AB1E, 19 bytes
Uses CP-1252 encoding.
nžs¦¦¹ôI£íRvyN>£J}R
Try it online!
Explanation
n=5, y=3
used for example
nžs # push pi to n^2 digits
# STACK: 3.1415926535897932384626433
¦¦ # remove the first 2 chars
# STACK: 1415926535897932384626433
¹ô # split into n*n matrix
# STACK: ['14159', '26535', '89793', '23846', '26433']
I£ # keep the first y rows
# STACK: ['14159', '26535', '89793']
íR # reverse the list of rows and each individual row
# STACK: ['39798', '53562', '95141']
v } # for each y,N (row, index) in the list
yN>£J # keep the first index+1 digits of the row and join to string
# STACK: 353951
R # reverse the string
# STACK: 159353
# implicit print
Python 2 (with sympy), 100 bytes
from sympy import*
lambda n,y:''.join(c for i,c in enumerate(`pi.round(n*n+1)`[2:])if i%n-i/n>n-y-1)
No sympy, 260 246 244 233 231 218 bytes
p=lambda n,y,a=-30,b=10,c=3,d=2,e=0,f=5,s='',i=0:i<n*n and p(n,y,*[((2*b+a)*f,b*d,c*f,d+1,(b*(7*d)+2+(a*f))/(c*f),f+2,s,i),(10*(a-e*c),10*b,c,d,((10*(3*b+a))/c)-10*e,f,s+(str(e)[:i%n-i/n>n-y-1]),i+1)][4*b+a-c<e*c])or s
This employs "The Spigot Algorithm For Pi" of Stanley Rabinowitz and Stan Wagon.
The standard arguments would be a,b,c,d,e,f=0,1,1,1,3,3
to yield the first digit of pi, 3
, since that is not required the algorithm is initialised to the point before 1
is yielded, which saves two bytes even though a
and b
are longer as the result does not require slicing and i
can start at 0
rather than -1
.
Hits default recursion limit for last test case
Uses //
for the first of the divisions so that str(v)
may be replaced by `v`
(otherwise it would end in L
for a long).
repl.it
A non-recursive version for 232 bytes which evaluates the last test case too:
def p(n,y):
a,b,c,d,e,f,i,s=-30,10,3,2,0,5,0,''
while i<n*n:
if 4*b+a-c<e*c:s+=`e`[:i%n-i/n>n-y-1];g=10*(a-e*c);e=((10*(3*b+a))//c)-10*e;b*=10;i+=1
else:g=(2*b+a)*f;h=(b*(7*d)+2+(a*f))/(c*f);b*=d;c*=f;f+=2;d+=1;e=h
a=g
print s
repl.it (first indent is one space, second indent is one tab)
Mathematica, 82 bytes
Print@@Join@@Partition[RealDigits[Pi-3,10,#^2][[1]],#][[i,i-#2-1;;]]~Table~{i,#2}&