• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Formula Checking Code

SOLinus Aug 24th, 2016 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #see http://codegolf.stackexchange.com/a/90883/46756
2.
3. #define a working list to fill with representations which evaluate
4. #to given values, and parameters for filling that list.
5. m=11111      #bound for list
6. R=range(0,m) #list for representations
7. U=range(0,m) #code to see which formula was used
8. R[1]="()"    #intiallize list
9. R[2]="()()"
10. l=2          #current least new represtation
11.
12. #define a method to add a representation for a value to the list
13. def a(v,r,u):
14.  if v>0 and v<m:
15.   if isinstance(R[v],int) or len(r)<len(R[v]):
16.    R[v]=r
17.    U[v]=-u
18.    if v<R[0]:
19.     R[0]=v
20.
21. #define a sumation function
22. def s(v,k):
23.  S=0
24.  while v>0:
25.   S+=v
26.   v-=k
27.  return S
28.
29. #shorthand for recurring brainflak structures
30. p=lambda r:"("+r+")"         #store
31. w=lambda r:"{({}["+r+"])}{}" #sum with increment
32. def q(r,v):                  #multiply by v
33.  for i in range(1,v):
34.   r="("+r+")"
35.  for i in range(1,v):
36.   r+="{}"
37.  return r
38. def e(r,v,k):                #multiply by expontent v^k
39.  for i in range(0,k):
40.   r=q(r,v)
41.  return r
42.
43. #fill the array with new representations formed from existing
44. #representations, if shorter.
45. while l<m:
46.  R[0]=l+1
47.  #multiplications
48.  a(l*2,q(R[l],2),1)
49.  a(l*3,q(R[l],3),2)
50.  a(l*5,q(R[l],5),3)
51.  a(l*7,q(R[l],7),4)
54.  for i in range(1,l):
56.   a(l+i,R[l]+R[i],7)
57.   #subtraction
58.   a(l-i,R[l]+"["+R[i]+"]",8)
59.   if 2*i<=l and l%i==0:#2*i<=l should save some time, is not golfed
60.    t=s(l-i,i)
61.    #sumations
62.    a(s(l,i),p(R[l])+w(R[i]),9)
63.    #multiples of sums, plus
64.    a(l+2*t,p(R[l])+q(w(R[i]),2),11)
65.    a(l+4*t,p(R[l])+e(w(R[i]),2,2),12)
66.    a(l+8*t,p(R[l])+e(w(R[i]),2,3),13)
67.    a(l+16*t,p(R[l])+e(w(R[i]),2,4),14)
68.    a(l+32*t,p(R[l])+e(w(R[i]),2,5),15)
69.    a(l+64*t,p(R[l])+e(w(R[i]),2,6),16)
70.    a(l+128*t,p(R[l])+e(w(R[i]),2,7),17)
76.
77.    a(l+3*t,p(R[l])+q(w(R[i]),3),23)
78.    a(l+9*t,p(R[l])+e(w(R[i]),3,2),24)
79.    a(l+27*t,p(R[l])+e(w(R[i]),3,3),25)
83.
84.    a(l+5*t,p(R[l])+q(w(R[i]),5),29)
87.
90.
92.
93.    a(l+6*t,p(R[l])+q(q(w(R[i]),3),2),35)
94.    a(l+10*t,p(R[l])+q(q(w(R[i]),5),2),36)
97.    a(l+15*t,p(R[l])+q(q(w(R[i]),5),3),39)
103.
104.    a(l+12*t,p(R[l])+q(q(q(w(R[i]),3),2),2),45)
105.    a(l+18*t,p(R[l])+q(q(q(w(R[i]),3),3),2),46)
106.    a(l+20*t,p(R[l])+q(q(q(w(R[i]),5),2),2),47)
120.
124.
125.    a(l+24*t,p(R[l])+q(q(q(q(w(R[i]),3),2),2),2),64)
126.    a(l+36*t,p(R[l])+q(q(q(q(w(R[i]),3),3),2),2),65)
128.    a(l+40*t,p(R[l])+q(q(q(q(w(R[i]),5),2),2),2),67)
133.
134.  l=R[0]
135.
136. #return final representation of a value
137. f=lambda v:p(R[v])
138.
139.
140. #THIS ENDS THE GOLFED REPRESENTATION PART OF THE PROGRAM
141. def isPrime(v):
142.  i=2
143.  while i*i<=v:
144.   if v%i==0:
145.    return False
146.   i+=1
147.  return True
148.
149. for i in range(1,m):
150.  #if isPrime(i):, while testing which formula are used, since
151.  #only the last formula used is shown we need to see all numbers
152.   print str(U[i])+" "+f(i)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top