Added solutions for N=50-60

master
Tibor Bizjak 2023-03-27 17:26:44 +02:00
parent fab507d468
commit 073423a316
9 changed files with 1310 additions and 0 deletions

View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
#longest consecutive sum of primes that is a prime itself <x
def primes(x):
#pra stevila do xa
nums=[True]*x
primes=[2]
i=3
while i<x:
if nums[i-1]==True:
primes.append(i)
j=i*2
while j<x:
nums[j-1]=False
j+=i
i+=2
sums=[]
prime=0
m=1
for i in primes:
sums=list(map(lambda x: x+i,sums))
sums.append(i)
a=-2
while abs(a)<=len(sums):
try:
if nums[sums[a]-1]==True and abs(a)>m:
prime=sums[a]
m=abs(a)
except:
sums=sums[a+1:]
break
a-=1
return prime
if __name__ == '__main__':
print(primes(10**6))

View File

@ -0,0 +1,49 @@
#!/usr/bin/env python3
def primes(x,y):
#pra stevila do xa
nums=[True]*x
primes=[2]
i=3
roots={}
dig=1
poss=[]
while i<x:
if i>=10**dig:
if poss:
poss=[n for n in poss if len(n)==y]
if poss:
return min(map(min, poss))
dig+=1
roots={}
if nums[i-1]==True:
n=str(i)
roll=[]
for r in set(n):
j=0
while n[j]!=r:
j+=1
roll.extend(dig_replacement([n[:j]+"*",n[:j+1]],n[j+1:],r))
for j in roll:
l=roots.setdefault(j,[])
l.append(i)
if len(l)==y:
poss.append(l)
primes.append(i)
j=i*2
while j<x:
nums[j-1]=False
j+=i
i+=2
return False
def dig_replacement(starts,end,num):
i=0
while i<len(end):
if end[i]==num:
return dig_replacement([j+end[:i]+"*" for j in starts]+[j+end[:i+1] for j in starts],end[i+1:],num)
i+=1
return [j+end for j in starts][:-1]
if __name__ == '__main__':
print(primes(2 * 10**6, 8))

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
def perm():
#najde najmasno stevilo katerega produkti z 1..6 so permutacije
a=100
b=166
while True:
i=a
while i<=b:
count=1
roll=i*2
while sorted(str(i))==sorted(str(roll)) and count<6:
count+=1
roll+=i
if count==6:
return i
i+=1
a*=10
b=(b*10)+6
return False
if __name__ == '__main__':
print(perm())

92
054_poker_hands.py 100644
View File

@ -0,0 +1,92 @@
#!/usr/bin/env python3
def final():
r=block_to_list("data/054_poker.txt")
count=0
for i in r:
if compet(i[0],i[1]):
count+=1
return count
def compet(x,y):
a=pok_hand(x)
b=pok_hand(y)
av=[n for n in a]
bv=[n for n in b]
av.sort()
bv.sort()
i=1
while i<=len(av) and i<=len(bv):
if av[-i]!=bv[-i]:
if av[-i]>bv[-i]:
return True
else:
return False
for n in range(len(a[av[-i]])):
if a[av[-i]][n]!=b[bv[-i]][n]:
if a[av[-i]][n]>b[bv[-i]][n]:
return True
else:
return False
i+=1
def block_to_list(fn):
result=[]
with open(fn) as f:
for line in f:
s=line.split(" ")
result.append([s[:5],s[5:9]+[s[9][:2]]])
return result
def pok_hand(x):
v={'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'T':10,'J':11,'Q':12,'K':13,'A':14}
suit=[n[1] for n in x]
val=[v[n[0]] for n in x]
high_com={1:sorted(val,reverse=True)}
if len(set(suit))==1:
#flush
high_com[6]=[0]
if len(set(val))<5:
d={i:0 for i in set(val)}
for i in val:
d[i]+=1
for k,v in d.items():
if v>1:
if v==2:
#pair
if not(2 in high_com):
high_com[2]=[k]
else:
high_com[2].append(k)
elif v==3:
#tris
high_com[4]=[k]
elif v==4:
#four of kind
high_com[8]=[k]
if (2 in high_com) and len(high_com[2])==2:
#two pairs
high_com[3]=sorted(high_com[2],reverse=True)
del high_com[2]
elif (2 in high_com) and (4 in high_com):
#full house
high_com[7]=[high_com[4],high_com[2][0]]
del high_com[4]
del high_com[2]
elif sorted(val)==list(range(sorted(val)[0],sorted(val)[0]+5)):
if 6 in high_com:
del high_com[6]
if sorted(val)[0]==10:
#royal_flush
high_com[10]=[0]
else:
#staright flsuh
high_com[9]=[sorted(val)[-1]]
else:
#straight
high_com[5]=[sorted(val)[-1]]
return high_com
if __name__ == '__main__':
print(final())

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
def lychrel(x):
ly=[]
i=1
while i<x:
num=i
count=0
while count<50:
num=num+analogize(digitize(num)[::-1])
if pal_check(num):
break
count+=1
else:
ly.append(i)
i+=1
return ly
def analogize(x):
i=0
num=0
while i<len(x):
num+=x[i]*(10**i)
i+=1
return num
def pal_check(x):
d=digitize(x)
if d==d[::-1]:
return True
return False
def digitize(x):
if x<10:
return [x]
return [x%10]+digitize(x//10)
if __name__ == '__main__':
print(len(lychrel(10**4)))

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
def power(x):
#brute force, ni glih nek algo.. jbg, deluje
a=2
maxim=0
while a<x:
b=1
while b<x:
k=sum(digitize(a**b))
if k>maxim:
maxim=k
b+=1
a+=1
return maxim
def digitize(x):
if x<10:
return [x]
return [x%10]+digitize(x//10)
if __name__ == '__main__':
print(power(100))

View File

@ -0,0 +1,47 @@
#!/usr/bin/env python3
def decrypt():
#genereta tri mestni kluc (mozne so samo male crke)
#decrypta podano besedilo enc_text in ga vrze v percentege
#ce je vec kot 50% besed angleskih ga doda na list results
eng=text()
results=[]
with open("data/059_cipher.txt") as f:
r=f.read()
enc_text=list(map(int,r.split(",")))
key=[]
for a in range(97,123):
for b in range(97,123):
for c in range(97,123):
dec_text=""
for char in range(0,len(enc_text)-2,3):
if enc_text[char]^a in range(0, 128):
dec_text+=chr(enc_text[char]^a)
if enc_text[char+1]^b in range(0, 128):
dec_text+=chr(enc_text[char+1]^b)
if enc_text[char+2]^c in range(0, 128):
dec_text+=chr(enc_text[char+2]^c)
if percentege(dec_text.upper().split(" "),eng)>50:
results.append(dec_text)
key.append([a,b,c])
return results[0]
def text():
with open("data/098_words.txt") as f:
r=f.read()
r=r.split('","')
r[0]=r[0][1:]
r[-1]=r[-1][:-1]
return r
def percentege(x,y):
#x je besedilo, y je list vecine ang besed, vrne ujemanje obeh v %
total=0
for i in x:
if i in y:
total+=1
return (float(total)/len(x))*100
if __name__ == '__main__':
text = decrypt()
print(sum(map(ord, text)))

1000
data/054_poker.txt 100644

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
36,22,80,0,0,4,23,25,19,17,88,4,4,19,21,11,88,22,23,23,29,69,12,24,0,88,25,11,12,2,10,28,5,6,12,25,10,22,80,10,30,80,10,22,21,69,23,22,69,61,5,9,29,2,66,11,80,8,23,3,17,88,19,0,20,21,7,10,17,17,29,20,69,8,17,21,29,2,22,84,80,71,60,21,69,11,5,8,21,25,22,88,3,0,10,25,0,10,5,8,88,2,0,27,25,21,10,31,6,25,2,16,21,82,69,35,63,11,88,4,13,29,80,22,13,29,22,88,31,3,88,3,0,10,25,0,11,80,10,30,80,23,29,19,12,8,2,10,27,17,9,11,45,95,88,57,69,16,17,19,29,80,23,29,19,0,22,4,9,1,80,3,23,5,11,28,92,69,9,5,12,12,21,69,13,30,0,0,0,0,27,4,0,28,28,28,84,80,4,22,80,0,20,21,2,25,30,17,88,21,29,8,2,0,11,3,12,23,30,69,30,31,23,88,4,13,29,80,0,22,4,12,10,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,65,69,83,80,84,87,68,69,83,80,84,87,73,69,83,80,84,87,65,83,88,91,69,29,4,6,86,92,69,15,24,12,27,24,69,28,21,21,29,30,1,11,80,10,22,80,17,16,21,69,9,5,4,28,2,4,12,5,23,29,80,10,30,80,17,16,21,69,27,25,23,27,28,0,84,80,22,23,80,17,16,17,17,88,25,3,88,4,13,29,80,17,10,5,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,12,11,80,10,26,4,4,17,30,0,28,92,69,30,2,10,21,80,12,12,80,4,12,80,10,22,19,0,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,69,30,31,9,20,31,18,11,94,69,54,17,8,29,28,28,84,80,44,88,24,4,14,21,69,30,31,16,22,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,25,22,88,17,69,11,25,29,12,24,69,8,17,23,12,80,10,30,80,17,16,21,69,11,1,16,25,2,0,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,23,22,69,12,24,0,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,67,80,10,10,80,7,1,80,21,13,4,17,17,30,2,88,4,13,29,80,22,13,29,69,23,22,69,12,24,12,11,80,22,29,2,12,29,3,69,29,1,16,25,28,69,12,31,69,11,92,69,17,4,69,16,17,22,88,4,13,29,80,23,25,4,12,23,80,22,9,2,17,80,70,76,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,11,80,17,23,80,84,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,12,31,69,12,24,0,88,20,12,25,29,0,12,21,23,86,80,44,88,7,12,20,28,69,11,31,10,22,80,22,16,31,18,88,4,13,25,4,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,17,23,80,7,29,80,4,8,0,23,23,8,12,21,17,17,29,28,28,88,65,75,78,68,81,65,67,81,72,70,83,64,68,87,74,70,81,75,70,81,67,80,4,22,20,69,30,2,10,21,80,8,13,28,17,17,0,9,1,25,11,31,80,17,16,25,22,88,30,16,21,18,0,10,80,7,1,80,22,17,8,73,88,17,11,28,80,17,16,21,11,88,4,4,19,25,11,31,80,17,16,21,69,11,1,16,25,2,0,88,2,10,23,4,73,88,4,13,29,80,11,13,29,7,29,2,69,75,94,84,76,65,80,65,66,83,77,67,80,64,73,82,65,67,87,75,72,69,17,3,69,17,30,1,29,21,1,88,0,23,23,20,16,27,21,1,84,80,18,16,25,6,16,80,0,0,0,23,29,3,22,29,3,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,35,23,28,9,23,7,12,22,23,69,25,23,4,17,30,69,12,24,0,88,3,4,21,21,69,11,4,0,8,3,69,26,9,69,15,24,12,27,24,69,49,80,13,25,20,69,25,2,23,17,6,0,28,80,4,12,80,17,16,25,22,88,3,16,21,92,69,49,80,13,25,6,0,88,20,12,11,19,10,14,21,23,29,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,29,80,22,29,2,12,29,3,69,73,80,78,88,65,74,73,70,69,83,80,84,87,72,84,88,91,69,73,95,87,77,70,69,83,80,84,87,70,87,77,80,78,88,21,17,27,94,69,25,28,22,23,80,1,29,0,0,22,20,22,88,31,11,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,75,88,62,4,21,21,9,1,92,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,65,64,69,31,25,19,29,3,69,12,24,0,88,18,12,9,5,4,28,2,4,12,21,69,80,22,10,13,2,17,16,80,21,23,7,0,10,89,69,23,22,69,12,24,0,88,19,12,10,19,16,21,22,0,10,21,11,27,21,69,23,22,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,36,22,20,69,26,9,69,11,25,8,17,28,4,10,80,23,29,17,22,23,30,12,22,23,69,49,80,13,25,6,0,88,28,12,19,21,18,17,3,0,88,18,0,29,30,69,25,18,9,29,80,17,23,80,1,29,4,0,10,29,12,22,21,69,12,24,0,88,3,16,21,3,69,23,22,69,12,24,0,88,3,16,26,3,0,9,5,0,22,4,69,11,21,23,17,21,22,88,25,11,88,7,13,17,19,13,88,4,13,29,80,0,0,0,10,22,21,11,12,3,69,25,2,0,88,21,19,29,30,69,22,5,8,26,21,23,11,94