project-euler/061_cyclical_fig_nums.py

54 lines
1.2 KiB
Python
Raw Normal View History

2023-03-27 16:30:58 +02:00
#!/usr/bin/env python3
from math import sqrt
def cynical():
#main
i=1+int((2+sqrt(4+12*1009))/6)
o=3*i**2-2*i
while o<10000:
base=(o-(o//100)*100)*100
if base>=1000:
a=curse(base,o//100,[])
if a!=False:
return a+[o]
i+=1
o=3*i**2-2*i
return False
def curse(base,last,sit):
#recursive
if len(sit)==4:
if fig(base+last)!=False and not(fig(base*100+last) in sit):
return [base+last]
else:
i=10
while i<100:
if fig(base+i)!=False and not(fig(base+i) in sit):
a=curse(i*100,last,sit+[fig(base+i)])
if a!=False:
return a+[base+i]
i+=1
return False
def fig(x):
#pocekira ce je x katera izmed figurative stevil
if int(sqrt(x))==sqrt(x):
return 2
k=(1+sqrt(1+24*x))/3
if int(k)==k:
return 3
k=sqrt(1+8*x)
if int(k)==k:
k=(1+sqrt(1+8*x))/4
if int(k)==k:
return 4
return 1
k=(3+sqrt(9+40*x))/5
if int(k)==k:
return 5
return False
if __name__ == '__main__':
print(sum(cynical()))