Question

Given a real number x and a positive integer k, determine the number of multiplications used...

Given a real number x and a positive integer k, determine the number of multiplications used to find x^(2^i) starting with x and successively squaring ( to find x^2, x^4 and so on). Is this a more efficient way to find x^(2^i) than by multiplying x by itself the appropriate number of times?
Give the C++ code to work this problem, if possible with recursion, if not explain why?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <iostream>
using namespace std;
double calculate(double x,int k)
{
if(k==0)
return x;
return calculate(x*x,k-1);
}
int main()
{

cout<<"Enter x: ";
double x;
int k;
cin>>x;
cout<<"Enter k: ";
cin>>k;
cout<<"x^2^k is "<<calculate(x,k)<<endl;
return 0;

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Given a real number x and a positive integer k, determine the number of multiplications used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT