0% found this document useful (0 votes)
68 views5 pages

Recursivitate: DEF: Un Enunt Recursiv Este Un Enunt Care Se Autodefineste

- Recursion is a fundamental concept that involves defining a problem in terms of itself through recursive calls. There must be a base case with an immediate solution and a way to reduce the problem size towards the base case. - Examples of recursive functions include calculating the sum of digits of a number, finding terms in the Fibonacci sequence, checking if a number is a Fibonacci term, calculating factorials, exponents, greatest common divisor, maximum/minimum digit of a number, and checking if a number is prime. - Recursive solutions break down problems into sub-problems of the same type until a base case is reached, while iterative solutions use loops to solve the problem without recursive calls.

Uploaded by

Iulia Zaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Recursivitate: DEF: Un Enunt Recursiv Este Un Enunt Care Se Autodefineste

- Recursion is a fundamental concept that involves defining a problem in terms of itself through recursive calls. There must be a base case with an immediate solution and a way to reduce the problem size towards the base case. - Examples of recursive functions include calculating the sum of digits of a number, finding terms in the Fibonacci sequence, checking if a number is a Fibonacci term, calculating factorials, exponents, greatest common divisor, maximum/minimum digit of a number, and checking if a number is prime. - Recursive solutions break down problems into sub-problems of the same type until a base case is reached, while iterative solutions use loops to solve the problem without recursive calls.

Uploaded by

Iulia Zaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

RECURSIVITATE

- concept fundamental
- regula pentru o recursivitate corecta
- trebuie sa existe un caz care are o solotie imediata, elementara
- pentru problemele care nu au o solutie elementara trebuie sa existe o
dezvoltare a problemei spre o solutie elementara
- DEF: un enunt recursiv este un enunt care se autodefineste

Varianta iterativa Varianta recursiva


1.Suma cifrelor unui numar intreg pozitiv int sumaR(int x)
{
int sumacif(int x) int(x==0) return 0;
{ else
int s=0; return x%10 + sumaR(x/10);
while(x>0) }
{
s=s+x%10; s(326) = 6+s(32) = 6+5 = 11
x/=10; s(32) = 2+s(3) = 2+3 = 5
} s(3) = 3+s(0) = 3+0 = 3
return s; s(0) = 0
}
0
3
32
326 = nivelul 1

2. Sirul Fibonacci int fiboR(int n)


f(n) = 1 daca n<=2 (1 1/0 1) {
f(n-1)+f(n-2) daca n>2 if(n<=2)
return 1;
Temenul nr n din sirul Fibonacci else
return fiboR(n-1)+fiboR(n-2);
int fibo(int n) }
{
if(n<=2)
return 1;
int f1, f2, f3, I; f(5)
f1=f2=1; f(6)
for(i=3; i<=n; i++) f(4)
{ f(7)
f3=f1+f2; f(4)
f1=f2; f(5)
f2=f3; f(3)
}
return f3;
}

3. Verificare x=termen Fibonacci int efiboR(intx, int f1, int f2)


1 1 2 3 5 8 13 21 34 55 {
int f3=f1+f2;
int efibo(int x) if(f3==x)
{ return 1;
if(x==1) else
return 1; if(f3>x)
int f1, f2, f3; return 0;
f1=f2=1; else
f3=f1=f2; return efibR(x, f2, f3)
while(f3<=x) }
{
f1=f2; !!! apelul:
f2=f3; if(efibo(x, 0, 1)==1)
f3=f1+f2;
}
if(f2==x)
return 1;
else
return 0;
}
3. Factorul n! = 1*2**n int factR(int n)
{
int fact(int n) if(n==0) return 1;
{ else
int p, I; return n*factR(n-1);
p=1; }
for(i=1; i<=n; i++)
p=p*I;
return p;
}

0!=1
4. Puterea lui a int putereR(int a, int n)
an = a*a**a de n ori {
if(n==0) return 1;
int putere(int a, int n) else
{ return a*putereR(a, n-1);
int p=1; }
for(int i=1; i<=n; i++)
p*=a;
return p;
}
5. Cel mai mare divizor comun

Diferenta

cmmdc(a, b) = a daca a=b;


= cmmdc(a-b, b) daca
a>b;
= cmmdc(a, b-a) daca
a<b;
int Dif(int a, int b) int DifR(int a, int b)
{ {
if(a==0 && b==0) return INT_MIN; if(a==b) return a;
(!!!) else
else if(a>b) return DifR(a-b, b);
if(a!=0) return a; else
else return DifR(a, b-a);
if(b!=0) return b; }
while(a!=b)
if(a>b) a=a-b;
else b=b-a;
return a;
}

Algoritmul lui Euclid


int EuclidR(int a, int b)
int Euclid(int a, int b) {
{ if(a%b==0) return b;
if(a==0 && b==0) return INT_MIN; else return EuclidR(b, a%b);
(!!!) }
else
if(a!=0) return a;
else
if(b!=0) return b;
int r=a%b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}
return b;
}
6. Maximul dintre cifrele unui numar void maxcR(int x, int &mc)
intreg pozitiv {
if(x!=0)
int maxc(int x) {
{ if(x%10>mc) mc=x%10;
int mc=0; maxcR(x.10, mc);
while(x>0) }
{ }
if(x%10>mc) mc=x%10;
x/10; apelul;
} int mc=0;
return mc; maxcR(x, mc);
} cout<<mc

int maxcR(int x)
{
int(x==0) return 0;
else
{
int c=maxcR(x/10)
if(x%10>c) return x%10;
else return c;
}
}

Minimul dintre cifrele lui x!=0


int mincR(intx)
{
if(x==0) retun 9;
else
{
int c=mincR(x/10);
if(x%10<c) return x%10;
else return c;
}
}

int mincR(int x)
{
if(x<=9) return x;
else
{
int c=mincR(x/10);
if(x%10<c) return x%10;
else return c;
}
}
7. Verificare nr prim int primR(int x)
{
int prim(int x) if(x<=1 || x%2==0 && x!=2) return 0;
{ else
if(x<=1 || x%2==0 && x!=2) return 0; if(d*d<=x)
for(int d=3; d*d<=x; d=d+2) if(x%d==0) return 0;
if(x%d==0) else
return 0; return prim(x, d+2);
return 1; else
} return 1;
}

apelul: if(pim(x, 3)==1)

int primR(int x)
{
if(x<=1) return 0;
else
if(d*d<=x)
if(x%d==0) return 0;
else
return prim(x, d+2);
else
return 1;
}

apelul: if(prim(x, 2)==1)

OBS: !!!
prim(x, d+1) NU prim(x, d++)

You might also like