Algoritmi fundamentali

In [ ]:
#CMMDC - cel mai mare divizor comun
## Algorimul impartirilor repetate
x, y = map(int, input().split())
while x%y != 0:
    r=x%y
    x=y
    y=r
print(f"Cel mai mare divizor comun este: {y}")
In [ ]:
#CMMDC - cel mai mare divizor comun
## Algorimul scaderilor repetate
x, y = map(int, input().split())
while x!=y:
    if x>y:
        x=x-y
    else:
        y=y-x
else:
    print("cel mai mare divizor comun este:",x)
In [ ]:
#CMMMC - cel mai mic multiplu comun
x, y = map(int, input().split())
a=x; b=y
while x%y != 0:
    r=x%y
    x=y
    y=r
else:
    print(f"cel mai mic multiplu comun este:{(a*b)//y}")
In [ ]:
# --- sau --- #
#CMMMC - cel mai mic multiplu comun
x, y = map(int, input().split())
a=x; b=y
while x!=y:
    if x<y:
        x=x+a
    else:
        y=y+b
else:
    print("cel mai mic multiplu comun este:",x)
In [ ]:
#Oglinditul unui numar
n=int(input("n="))
ogl=0
while n>0:
    ogl=ogl*10+n%10
    n=n//10
else:
    print(ogl)
In [ ]:
#Palindrom
n=int(input("n="))
ogl=0; x=n
while n>0:
    ogl=ogl*10+n%10
    n=n//10
else:
    if x==ogl:
        print("Palindrom")
    else:
        print("Nu este palindrom")
In [7]:
#Divizorii unui numar
n=int(input("n="))
for i in range(1,n+1):
    if n%i==0:
        print(i,end=" ")
1 2 3 4 6 9 12 18 36 
In [ ]:
#Divizorii proprii
n=int(input("n="))
for i in range(2,n//2+1):
    if n%i==0:
        print(i,end=" ")
In [ ]:
#Optimizare pentru numarul si suma tuturor divizorilor unui numar
n=int(input("n="))
k=0; s=0; i=1
while i*i<n:
    if n%i==0:
        k=k+2
        s=s+i+n//i
    i=i+1
if i*i==n:
    k=k+1
    s=s+i
print(k,s,end=" ")
In [ ]:
#Testul de numar prim
n=int(input("n="))
ok=True
if n<2 or (n%2==0 and n>2):
    ok=False
d=3
while d*d<=n and ok==True:
    if n%d==0:
        ok=False
    d=d+2
else:
    if ok==True:
        print("Numar prim")
    else:
        print("Numarul nu este prim")
In [ ]:
#Descompunerea unui numar in produs de factori primi
n=int(input())
d=2
while n>1:
    k=0
    while n%d==0:
        k+=1
        n//=d
    if k>0:
        print(d,'^',k)
    d+=1
    if d*d>n and n>1:
        print(n,'^',1)
        break
In [ ]:
#Conversia unui numar din baza 10 in baza q
n, q = map(int, input().split())
nq=0; p=1
while n:
    nq=nq+p*(n%q)
    p=p*10
    n=n//q
else:
    print(nq)
In [ ]:
#Conversia unui numar din baza q in baza 10
n, q = map(int, input().split())
n10=0; p=1
while n:
    n10=n10+p*(n%10)
    p=p*q
    n=n//10
else:
    print(n10)