Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
this is a code to generate a list of integers using only nests of the operations: factorial, square-root and floor, on the number 3. Also, finds a pair of the integers which approximates pi.
import math as m
L=[(3,'3',0,'n')] #list of values attainable with nested roots and factorials
#the first 3 is the value of the element, the string '3' is the path from 3 to that number, the 0 is the length of the path from 3 to that number, the n indicates that the element hasn't yet been rooted or factorial'ed
M=[] #temporary storage to be integrated into L in order to see which elements have been used in each pass
i=1 #index of number of passes over L
n=5000 #number of passes over L (with each pass, the current numbers in L are rooted or factorial'ed)
NS=10**100 #upper limit of numbers for square rooting
NF=1000 #upper limit of numbers for factorial'ing
while i<n:
#FINDING THE FLOOR OF SQUARE ROOT OF EACH ELEMENT OF L
j=0 #j is the index of elements in each pass through L
while j<len(L):
if L[j][3]=='n' and L[j][0]<NS:
x=L[j][0] #value from current element
X=m.floor(m.sqrt(x))
p=L[j][1] #path from current element
P=p+'S'
t=0 #checking whether current X is already in M
for k in range(0,len(M)):
if M[k][0]==X: #(that X is already found in M)
t=t+1
if t==0:
M.append((X,P,i,'n'))
j=j+1
#FINDING THE FACTORIAL OF EACH ELEMENT OF L
j=0
while j<len(L): #factorial of each element of L
if L[j][3]=='n' and L[j][0]<NF:
x=L[j][0] #value from current element
X=m.factorial(x)
p=L[j][1] #path from current element
P=p+'F'
t=0
for k in range(0,len(M)):
if M[k][0]==X: #(that X is already found in M)
t=t+1
if t==0:
M.append((X,P,i,'n'))
L[j] = (x,p,i-1,'y')
j=j+1
j=0 #checking whether each element of M is in L
for j in range(0,len(M)):
t=0
for k in range(0,len(L)):
if M[j][0]==L[k][0]:
t=t+1
if t==0: #t==0 iff the j'th element of M isnt already found in L
L.append(M[j])
i=i+1
L=sorted(L, key=lambda x: x[0]) #sorted the list
for i in L: #printing the elements of the list
print(i)
print('------------------')
#finding the best approximation of pi
pi=m.pi
a=10
b=1
NQ = 10**200 #put cap on the size of numbers since computer can't find the quotient of large numbers
for i in range(0,len(L)): #running through all pairs of numbers in L to find the pair with the most accurate ratio for pi
for k in range(0,len(L)):
e=m.fabs(pi-a/b)
if L[i][0]<NQ and L[k][0]<NQ:
E=m.fabs(pi-L[i][0]/L[k][0])
if E<e:
a=L[i][0]
b=L[k][0]
print(a,b,a/b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.