30 and 60
Find the smallest 30-digit integer N which contains each of the digits 0 to 9 three times, whose square power is a 60-digit number which contains each of the digits 0 to 9 six times.
import time
time1=time.time()
''' This part should be changed for the problem.
stri,globalstop='31622',True
repeatcount,totalcount,allcount,mulcount=4,8,40,80
'''
def Check ():
global globalstop
for i in range(10):
if stri.count(str(i))>repeatcount:
return False
if len (stri)==allcount:
#print("hi",stri)
Num=str(int(stri)**2)
if len(Num)==mulcount:
count=0
for i in range(10):
if Num.count(str(i))!=totalcount:
break
else:
count+=1
if count>1:
print("stri=",stri,count,int(stri)**2)
print(time.time()-time1)
if count==10:
print("Found",stri)
print(time.time()-time1)
globalstop=False
else:
temp=stri
#Len=len(stri)-1
add=allcount-len(stri)
for i in range(add):
temp+='9'
Num1=str(int(temp)**2)
if len(Num1)!=mulcount:
print("Electing=",stri)
return False
keep=True
for j in range(9):
temp3=stri
for i in range(add):
temp3+=str(j)
temp1=int(temp3)**2
#print(j,temp1)
if str(temp1)[0]=='1':
keep=False
break
else:
print("Electing=",stri,j,temp1)
if keep==False:
break
count=0
Num2=str(temp1)
#print(stri,Num1,Num2,j)
for i in range(len(Num1)):
if Num1[i]==Num2[i]:
count+=1
else:
break
Len=count
if Len>0:
result=Num1[0:Len]
else:
return False
for i in range(10):
if result.count(str(i))>totalcount:
return False
#print(stri,len(stri),result)
return True
def Solve():
global stri,globalstop
if globalstop==False:
return
for i in range(10):
stri+=str(i)
if Check()and globalstop:
if len(stri)<allcount:
Solve()
stri=stri[0:len(stri)-1]
print("Starting with=",stri)
Solve()
print(time.time()-time1)
Actually, some variables should be changed to achieve the solution of the problem. This is kind of clever algorithm and it is obviously a recursive depth search. The crucial part of the algorithm is in the Check function.
What is beautiful here is to get the answer using Python, sure slower than C++ and Fast Fourier version but I have found the smallest square number with a proof.