Sunteți pe pagina 1din 10

Probleme pentru teza. Subprograme recursive 1. Sa se calculeze suma primelor n numere naturale: S ( n ) = 1 + 2 + 3 + ... + n ; n N .

. 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n , daca n > 0. # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma primelor "<<n<<" numere naturale este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & else return suma(m'1)(m $ ). Sa se calculeze suma patratelor primelor n numere naturale: S ( n ) = 12 + 22 + 32 + ... + n 2 ; n N . 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * n , daca n > 0. # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma patratelor primelor "<<n<<" numere naturale este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & else return suma(m'1)(m*m $ +. Sa se calculeze suma cuburilor primelor n numere naturale: S ( n ) = 13 + 23 + 33 + ... + n 3 ; n N . 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * n * n , daca n > 0.

# include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma cuburilor primelor "<<n<<" numere naturale este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & else return suma(m'1)(m*(m*m) $ ,. Sa se calculeze %actorialul numarului natural n: Fact ( n ) = 1g 2g 3g4g ...g n; n N. 1, daca n = 0; Rezolvare: Fact ( n ) = Fact ( n 1) * n , daca n > 0. # include <iostream.h> long double %act(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"-actorialul numarului "<<n<<" este: "<<%act(n) $ long double %act(unsigned int m) !i% (m##&) return 1 else return %act(m'1)*m $ .. Sa se calculeze produsul primelor n numere naturale pare: prod ( n ) = 2g4g 6g ...g (2g n ); n N . 1, daca n = 0; Rezolvare: prod ( n ) = 2, daca n = 1; prod ( n 1) * (2 * n ), daca n > 1. # include <iostream.h> long double prod(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Produsul primelor "<<n<<" "<<prod(n)

numere

naturale

pare

este:

$ long double prod(unsigned int m) !i% (m##&) return 1 else return prod(m'1)*()*m) $ /. Sa se calculeze produsul primelor n numere naturale impare: prod ( n ) = 1g 3g 5g ...g (2g n + 1); n N . 1, daca n = 0; Rezolvare: prod ( n ) = prod ( n 1) * ( 2 * n + 1) , daca n > 0. # include <iostream.h> long double prod(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Produsul primelor "<<n<<" numere naturale impare este: "<<prod(n) $ long double prod(unsigned int m) !i% (m##&) return 1 else return prod(m'1)*()*m(1) $ 0. Sa se calculeze suma alternanta a primelor n numere naturale:
n 1

S ( n ) = 1 2 + 3 4 + ... + ( 1)

g n; n N.

0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n , daca n este impar ; S ( n 1) n , daca n este par . # include <iostream.h> int suma(int m) void main() !int n cout<<"n#" cin>>n cout<<"Suma alternanta a primelor "<<n<<" numere naturale este: "<<suma(n) $ int suma(int m) !i% (m##&) return & else i% (m1)) return suma(m'1)(m else return suma(m'1)'m

$ 2. Sa se calculeze suma alternanta a patratelor primelor n numere naturale:


n 1

S ( n ) = 12 22 + 32 42 + ... + ( 1)

g n 2; n N.

0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * n , daca n este impar ; S ( n 1) n * n , daca n este par . # include <iostream.h> int suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma alternanta a patratelor primelor "<<n<<" numere naturale este: "<<suma(n) $ int suma(unsigned int m) !i% (m##&) return & else i% (m1)) return suma(m'1)(m*m else return suma(m'1)'m*m $ 3. Sa se calculeze suma alternanta a cuburilor primelor n numere naturale: S ( n ) = 13 23 + 33 43 + ... + ( 1)
n 1

g n 3; n N .

0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * n * n , daca n este impar ; S ( n 1) n * n * n , daca n este par . # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma alternanta a cuburilor primelor "<<n<<" numere naturale este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & else !i% (m1)) return suma(m'1)(m*(m*m) else return suma(m'1)'m*(m*m) $ $

1&. Sa se calculeze suma : S ( n ) = 1g 3 + 2g 5 + 3g 7 + ... + n g ( 2gn + 1) ; n N . 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * ( 2 * n + 1) , daca n > 0. # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n" cin>>n cout<<"Suma este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & return suma(m'1)(m*()*m(1) $ 11. Sa se calculeze suma :
n 1

S ( n ) = 1g 2 2g4 + 3g 6 4g8 + ... + ( 1)

n g( 2g n ) ; n N.

0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + n * ( 2 * n + 1) , daca n este impar ; S ( n 1) n * ( 2 * n + 1) , daca n este par . # include <iostream.h> long double suma(unsigned int m) void main() ! unsigned int n cout<<"n#" cin>>n cout<<"Suma este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return & i% (m1)) return suma(m'1)(m*()*m) else return suma(m'1)'m*()*m) $ 1). Sa se calculeze produsul primelor n numere naturale impare: prod ( n ) = 1g4g 7g 10g ...g (3g n 2); n N . 1, daca n = 0; Rezolvare: prod ( n ) = prod ( n 1) * ( 3 * n 2 ) , daca n > 0.

# include <iostream.h> long double prod(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Produsul este: "<<prod(n) $ long double prod(unsigned int m) !i% (m##&) return 1 else return prod(m'1)*(+*m')) $ 1+. Sa se calculeze suma primelor n numere naturale impare: S ( n ) = 1 + 3 + 5 + ... + ( 2g n + 1) ; n N . 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + ( 2 * n + 1) , daca n > 0. # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n#" cin>>n cout<<"Suma primelor "<<n<<" numere naturale impare este: "<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return 1 else return suma(m'1)()*m(1 $ 1,. Sa se calculeze suma primelor n numere naturale pare: S ( n ) = 2 + 4 + 6 + ... + ( 2g n ) ; n N. 0, daca n = 0; Rezolvare: S ( n ) = S ( n 1) + ( 2 * n ) , daca n > 0. # include <iostream.h> long double suma(unsigned int m) void main() !unsigned int n cout<<"n" cin>>n cout<<"Suma primelor "<<n<<" numere naturale pare este: "<<suma(n) $

long double suma(unsigned int m) !i% (m##&) return & else return suma(m'1)()*m $
k k n 1 1.. Sa se calculeze: Cn = Cn 1 + Cn 1 .

1, daca k = 0 sau n = k ; Rezolvare: comb ( n , k ) = n , daca k = 1; comb ( n 1, k ) + comb ( n 1, k 1) , in rest . # include <iostream.h> long double comb(unsigned int 45 unsigned int 6) void main() !unsigned int n5 7 cout<<"n#" cin>>n cout<<"7#" cin>>7 cout<<"8ombinari de "<<n<<" luate cate "<<7<<" "<<comb(n57) $ long double comb(unsigned int 45 unsigned int 6) !i% ((6##&)99(4##6)) return 1 else i% (6##1) return 4 else return comb(4'156)(comb(4'156'1) $ n k 1 . 1/. Sa se calculeze: Cnk = Cn k 1 1, daca k = 0 sau n = k ; Rezolvare: comb ( n , k ) = n comb ( n 1, k 1) , in rest . k # include <iostream.h> long double comb(unsigned int 45 unsigned int 6) void main() !unsigned int n5 7 cout<<"n#" cin>>n cout<<"7#" cin>>7 cout<<"8ombinari de "<<n<<" luate cate "<<7<<" "<<comb(n57) $ long double comb(unsigned int 45 unsigned int 6) !i% (6##&) return 4 else i% (4##6) return 1 else return (comb(4'156'1)*4):6 $

10. Sa se calculeze valoarea numerica a polinomului: P ( X ) = a0 + a1 X + a2 X 2 + ... + an X n , in puncul 4. a0 , daca n = 0; Rezolvare: pol (n , x ) = pol (n 1, x ) + an * pow ( x , n ) , daca n > 0. # include <math.h> %loat a;1&&< long double pol(unsigned int m5 %loat p) void main() !unsigned int n5i5= %loat 4 cout<<">radul polinomului:" cin>>n cout<<"4#" cin>>4 ::citirea coe%icientilor tabloului %or(i#& i<n i(() ! cout<<"a;"<<i<<"5"<<=<<"<#" cin>>a;i<;=< $ cout<<"?aloarea polinomului pentru "<<4<<" este: "<<pol(n54) $ long double pol(unsigned int m5 %loat p) !i% (m##&) return a;&< else return pol(m'15p)(a;m'1<*po@(p5m) $
n n 12. Sa se calculeze suma: S = x 1 + x 2 , n N . x 1 si x 2 sunt radacinile ecuatiei de

gradul AA ax 2 + bx + c = 0, cu coe%icienti reali. Rezolvare: x 1 si x 2 %iind radacini veri%ica ecuatia data. ax 12 + bx 1 + c = 0


2 ax 2 + bx 2 + c = 0

n 2 n 2 Anmultind prima relatie cu x 1 si a doua relatie cu x 2 5 prin adunare se obtine: n n 1 n 2 a x 1n + x 2 + b x 1n 1 + x 2 + c x 1n 2 + x 2 =0

aS ( n ) + bS ( n 1) + cS ( n 2 ) = 0 S (n) = b c S ( n 1) S ( n 2 ) . a a

adica de aici

si

2, daca n = 0; b Recursiv: S ( n ) = , daca n = 1; a b c S ( n 1) S ( n 2 ) , daca n . a a # include <iostream.h> %loat a5b5c long double suma(unsigned int m) void main() !unsigned int n cout<<"a#" cin>>a cout<<"b#" cin>>b cout<<"c#" cin>>c cout<<"n#" cin>>n cout<<"Suma radacinilor de grad "<<n<<" este:"<<suma(n) $ long double suma(unsigned int m) !i% (m##&) return ) else i% (m##1) return 'b:a else return 'b:a*suma(m'1)'c:a*suma(m')) $ an = an 1 + bn 1 13. Sa se calculeze termenii de rang n ai sirurilor: cu a0 si b0 an = an 1 bn 1 egali cu 1. # include <iostream.h> %loat a&5b& long double an(unsigned int m) long double bn(unsigned int m) void main() !unsigned int n a&#1 b&#1 cout<<"n#" cin>>n cout<<"Bermenul de rang "<<n<<" in sirul este:"<<an(n)<<endl cout<<"Bermenul de rang "<<n<<" in sirul bn este:"<<bn(n) $ long double an(unsigned int m) !i% (m##&) return a& else return an(m'1)(bn(m'1) $ long double bn(unsigned int m) !i% (m##&) return b&

an

else return an(m'1)'bn(m'1) $ )&. Sa se calculeze termenul de rang n din sirul lui -ibonacci: f n = f n 1 + f n 2 . 0, daca n = 0; Rezolvare: Fib ( n ) = 1, daca n = 1; Fib ( n 1) + Fib ( n 2 ) . # include <iostream.h> %loat %&5%1 long double %ib(unsigned int m) void main() !unsigned int n %&#& %1#1 cout<<"n#" cin>>n cout<<"Bermenul de rang "<<n<<" in sirul lui -ibonacci este:"<<%ib(n)<<endl $ long double %ib(unsigned int m) !i% (m##&) return %& i% (m##1) return %1 else return %ib(m'))(%ib(m'1) $