0% found this document useful (0 votes)
32 views1 page

Recursiv: Pe Biţi #Include Using Namespace STD Long P 1, X Int Main (Int I, K //X 2 K 5

The document describes two algorithms for calculating exponential values in logarithmic time: 1. A iterative algorithm that calculates the exponential a^b by multiplying a by itself b times, dividing b by 2 at each step. 2. A recursive algorithm that calculates the exponential a^b by recursively calling itself, halving b each time while multiplying a if b is even or multiplying the result by a if b is odd. Both algorithms take input values for a and b, calculate the exponential a^b, and output the result.

Uploaded by

Tic Cevm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views1 page

Recursiv: Pe Biţi #Include Using Namespace STD Long P 1, X Int Main (Int I, K //X 2 K 5

The document describes two algorithms for calculating exponential values in logarithmic time: 1. A iterative algorithm that calculates the exponential a^b by multiplying a by itself b times, dividing b by 2 at each step. 2. A recursive algorithm that calculates the exponential a^b by recursively calling itself, halving b each time while multiplying a if b is even or multiplying the result by a if b is odd. Both algorithms take input values for a and b, calculate the exponential a^b, and output the result.

Uploaded by

Tic Cevm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Algoritmul ar putea fi :

Pe biţi
#include <iostream>
#include <iostream>
using namespace std;
long p=1,x;
using namespace std;
int k; long p=1,x;
int main() int main()
{ { int i,k;
//k=6; //x=2; k=5;
//x=2;
cin>>k>>x;
cin>>k>>x;
for(i=0;(1<<i)<=k;++i)
while(k)
{if(k%2){p=p*x;}
{if ((1&k) p=p*x;
x=x*x; x=x*x;
k=k/2; }
} cout<<p;
cout<<p; return 0;
return 0;
}
}

recursiv
#include <iostream>
using namespace std;
// a^b calcul exponential in timp logaritmic
long ab(long a,long b)
{ if (b==0) return 1;
else{ if (b%2==0) return ab(a*a,b/2);
else return ab(a,b-1)*a;
}
}
int main()
{ long a,b,p;
cin>>a>>b;
p=ab(a,b);
cout<<endl<<p;
return 0; }

You might also like