180 lines
4.1 KiB
Python
180 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
def entiers(i,j): # Exercice 1
|
|
print('-'.join(map(str,list(range(i,j+1)))))
|
|
def entiers(i,j): # Exercice 2
|
|
retour=""
|
|
for i in range(i,j+1):
|
|
if i%7:
|
|
retour+=(str(i)+'-')
|
|
print(retour.rstrip('-'))
|
|
entiers(5,9)
|
|
|
|
def triangle1(n):
|
|
for i in range(n):
|
|
print('*'*(i+1))
|
|
triangle1(5)
|
|
print()
|
|
def triangle2(n):
|
|
for i in range(n):
|
|
print('*'*(n-i-1))
|
|
triangle2(5)
|
|
def pyramide1(n):
|
|
triangle1(n)
|
|
triangle2(n)
|
|
pyramide1(5)
|
|
print()
|
|
def pyramide2(n):
|
|
for i in range(n):
|
|
print(' '*(n-i)+' '.join('*'*(i+1))+' '*(n-i))
|
|
pyramide2(5)
|
|
|
|
def talkhys1(n):
|
|
for j,i in enumerate([int('1'*i) for i in range(1,n+1)]):
|
|
print(' '*(n-j)+str(i)+ ' x ' + str(i) + ' '*(n-j) + ' = ' + ' '*(n-j)+ str(i*i))
|
|
talkhys1(10)
|
|
|
|
def heure_to_sec(h,m,s):
|
|
return h*3600+m*60+s
|
|
def sec_to_heure(s):
|
|
return s//3600,(s%3600)//60, s%60
|
|
print(sec_to_heure(67))
|
|
|
|
def duree(*args):
|
|
return sec_to_heure(heure_to_sec(*args[3:]) - heure_to_sec(*args[:3]))
|
|
print(duree(5,3,20,10,3,39))
|
|
|
|
def rot13(entree):
|
|
texte=entree.lower()
|
|
alph="abcdefghijklmnopqrstuvwxyz"
|
|
return ''.join([alph[(alph.index(c)+13)%(len(alph))] if c in alph else c for c in texte])
|
|
print(rot13("har crefbaar abeznyr crafr dh'ha xvyb-bpgrg rfg étny à 1000 bpgrgf,ha vasbeznvgpvra rfg pbainvaph dh'ha xvybzèger rfg étny à 1024 zègerf."))
|
|
|
|
def isqrt(n):
|
|
p=n
|
|
while not(p*p <= n < (p+1)**2 or p<0):
|
|
p=p-1
|
|
return p
|
|
print(isqrt(65))
|
|
|
|
def isqrt2(n):
|
|
p=n
|
|
lr=0
|
|
while True:
|
|
p=p-1
|
|
i=0
|
|
r=0
|
|
while i<p:
|
|
r+=p
|
|
i+=1
|
|
if r <= n < lr :
|
|
return p
|
|
if p<0:
|
|
return
|
|
lr=r
|
|
print(isqrt2(66))
|
|
|
|
def exercice7(texte):
|
|
i=0
|
|
while True:
|
|
i+=1
|
|
if texte in str(2**i):
|
|
return i, 2**i
|
|
#print(exercice7("12112001"))
|
|
|
|
def premier(p):
|
|
if p<2:
|
|
return False
|
|
for k in range(2, isqrt(p)+1):
|
|
if p%k==0:
|
|
return False
|
|
return True
|
|
def npremiers(n):
|
|
premiers=[]
|
|
i=2
|
|
while len(premiers)<n:
|
|
if premier(i):
|
|
premiers.append(i)
|
|
i+=1
|
|
return premiers
|
|
#print(npremiers(1000))
|
|
def validegoldbach(n, premiers):
|
|
for k in premiers:
|
|
for l in premiers:
|
|
if k+l == n:
|
|
return True
|
|
if k+l > n:
|
|
break
|
|
return False
|
|
|
|
def goldbach(n):
|
|
premiers=npremiers(n)
|
|
for i in range(4, n+1, 2):
|
|
if not validegoldbach(i, premiers):
|
|
return False, i
|
|
return True
|
|
def valideconjecture(n, premiers, puissances):
|
|
for k in puissances:
|
|
for l in premiers:
|
|
if k+l==n:
|
|
return True
|
|
if k+ l > n:
|
|
break
|
|
if k > n:
|
|
break
|
|
return False
|
|
|
|
def conjecture(n):
|
|
premiers=npremiers(n)
|
|
puissances=[2**i for i in range(n)]
|
|
for i in range(3, n+1, 2):
|
|
if not valideconjecture(i, premiers, puissances):
|
|
return False, i
|
|
return True
|
|
print(goldbach(100))
|
|
print(conjecture(100))
|
|
|
|
def lookandsay(integer):
|
|
text = str(integer)
|
|
i = 0
|
|
res=""
|
|
while i < len(text):
|
|
c=1
|
|
j=0
|
|
while j < len(text[i+1:]):
|
|
if text[i+1:][j] == text[i]:
|
|
c+=1
|
|
else:
|
|
break
|
|
j+=1
|
|
res += str(c)+text[i]
|
|
i += j + 1
|
|
return int(res)
|
|
def conway(n, start=1):
|
|
un=start
|
|
for i in range(n-1):
|
|
un=lookandsay(un)
|
|
return un
|
|
v = 20 # 40
|
|
print(len(str(conway(v+1)))/len(str(conway(v))))
|
|
|
|
def shadok(n):
|
|
p_max=0
|
|
while 4**(p_max) < n:
|
|
p_max +=1
|
|
reste=n
|
|
print(p_max)
|
|
b4_re=[0]*(p_max)
|
|
for i in list(range(p_max))[::-1]:
|
|
for j in range(3, -1, -1):
|
|
if reste - j*(4**i) >= 0:
|
|
reste -= j*(4**i)
|
|
b4_re[i]=j
|
|
break
|
|
b4=reversed(b4_re)
|
|
|
|
dico=["ga", "bu", "zo", "meu"]
|
|
return '-'.join([dico[i] for i in b4])
|
|
def kodahs(texte):
|
|
dico=["ga", "bu", "zo", "meu"]
|
|
b4=[dico.index(t) for t in texte.split('-')]
|
|
return sum([i*(4**p) for p, i in enumerate(reversed(b4))]) |