473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Power calcualation once more :(

hi,

i already posted an entry because of this problem, unfortunately i
havent solved it so far..:(

I have created a recursion for the calculation of the power like this:

[code]
Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n) {
  2. if (n < 0)
  3. return 1/power(x, -n);
  4. else if (n == 0)
  5. return 1.0;
  6. else
  7. return x * power(x, n-1);
  8. }
  9.  
  10.  
But the problem ist that i should use this formula x^2n = x^n x^n in
any way in order to have a runtime of log(n).
I read in the previous posting something like i should use this:
Expand|Select|Wrap|Line Numbers
  1. if (0 == n%2)
  2. {
  3. return (x^((n-1)/2))*(x^((n-1)/2))
  4. }
  5. else
  6. {
  7. return x*(x^((n-1)/2))*(x^((n-1)/2))
  8. }
  9.  
Maybe someone could help me here once more in order to reach my final
aim...? :(

matti

Nov 6 '06 #1
9 1770
In article <11**********************@i42g2000cwa.googlegroups .com>,
<ma****@gmx.atwrote:
>hi,

i already posted an entry because of this problem, unfortunately i
havent solved it so far..:(

I have created a recursion for the calculation of the power like this:

[code]
Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n) {
  2.  if (n < 0)
  3.    return 1/power(x, -n);
  4.  else if (n == 0)
  5.    return 1.0;
  6.  else
  7.    return x * power(x, n-1);
  8. }

But the problem ist that i should use this formula x^2n = x^n x^n in
any way in order to have a runtime of log(n).
I read in the previous posting something like i should use this:
Expand|Select|Wrap|Line Numbers
  1. if (0 == n%2)
  2.   {
  3.        return (x^((n-1)/2))*(x^((n-1)/2))
  4.   }
  5.    else
  6.   {
  7.       return x*(x^((n-1)/2))*(x^((n-1)/2))
  8.   }
>Maybe someone could help me here once more in order to reach my final
aim...? :(
You're pretty close. Try:

if (0 == n%2) { double y = power(x,n/2); return y*y; }
else return x*power(x,n-1);

You may have been confused by the shorthand use of ^ which
shouldn't appear in your code.

Steve
Nov 6 '06 #2
hi,

ah you mean like that:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3. if (n < 0)
  4. return 1/power(x, -n);
  5. else if (n == 0)
  6. return 1.0;
  7.  
  8. if (0 == n%2)
  9. {
  10. double y = power(x,n/2);
  11. return y*y;
  12. }
  13. else
  14. return x*power(x,n-1);
  15. }
  16.  
Have you meant it in that way...?

matti

Nov 6 '06 #3
<ma****@gmx.atwrote:
>hi,

ah you mean like that:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3.  if (n < 0)
  4.    return 1/power(x, -n);
  5.  else if (n == 0)
  6.    return 1.0;
  7.   if (0 == n%2)
  8.   {
  9.        double y = power(x,n/2);
  10.        return y*y;
  11.   }
  12.   else
  13.        return x*power(x,n-1);
  14. }

Have you meant it in that way...?
Yes, that looks okay to me, but I haven't tried to run it.
Good luck.

Steve
>
matti

Nov 6 '06 #4


yes it runs.

one last question, shouldnt i modify this line:

return 1/power(x,-n);

as well, to reach the runtime of log(n)?

matti

Steve Pope wrote:
<ma****@gmx.atwrote:
hi,

ah you mean like that:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3.   if (n < 0)
  4.     return 1/power(x, -n);
  5.   else if (n == 0)
  6.     return 1.0;
  7.  
  8.    if (0 == n%2)
  9.    {
  10.        double y = power(x,n/2);
  11.        return y*y;
  12.    }
  13.    else
  14.        return x*power(x,n-1);
  15. }
  16.  
Have you meant it in that way...?

Yes, that looks okay to me, but I haven't tried to run it.
Good luck.

Steve

matti
Nov 6 '06 #5
<ma****@gmx.atwrote:
>yes it runs.
>one last question, shouldnt i modify this line:
>return 1/power(x,-n);
>as well, to reach the runtime of log(n)?
No, because that line gets run exactly once (and then only
if the input is negative). It will not affect the order
of your runtime.

S.
Nov 6 '06 #6
Steve Pope wrote:
>><ma****@gmx.atwrote:
>>>
Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3. if (n < 0)
  4.   return 1/power(x, -n);
  5. else if (n == 0)
  6.   return 1.0;
  7.  if (0 == n%2)
  8.  {
  9.        double y = power(x,n/2);
  10.        return y*y;
  11.  }
  12.  else
  13.        return x*power(x,n-1);
  14. }
ma****@gmx.at wrote:
>
yes it runs.

one last question, shouldnt i modify this line:

return 1/power(x,-n);

as well, to reach the runtime of log(n)?
1. Please don't top-post.
2. Remove any greetings from the posts you're replying to (they have no
informational content, so they just lengthen the post).
3. If you put an exponent of the form 2^n - 1 into your power function,
you still end up with a complexity of O(n), since the step
return x*power(x,n-1) will be called 1/2 * 2^n - 1 times.
I will give you the advice not to try to tackle this problem
recursively, as you will need to have all potences of x available (you
still can do this recursively, but the resulting code would be rather
messy). I won't spoil your fun of finding the right solution, so all I'm
saying is that the solution you have found so far is good enough.

Regards,
Stuart
Nov 7 '06 #7
Stuart Redmann <De*******@web.dewrote:
>>><ma****@gmx.atwrote:
Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3. if (n < 0)
  4.   return 1/power(x, -n);
  5. else if (n == 0)
  6.   return 1.0;
  7.  if (0 == n%2)
  8.  {
  9.        double y = power(x,n/2);
  10.        return y*y;
  11.  }
  12.  else
  13.        return x*power(x,n-1);
  14. }
>3. If you put an exponent of the form 2^n - 1 into your power function,
you still end up with a complexity of O(n), since the step
return x*power(x,n-1) will be called 1/2 * 2^n - 1 times.
That doesn't seem right. Any pair of consecutive calls
beginning with power(n) must reduce its argument by at least a factor
of 2 since the only possibilities are:

n = 2*k (n even) which results in a call to power(k)

n = 2*k+1 (n odd) which results in a call to power(2*k), then a
call to power(k)

Therefore the runtime is bounded by 2 * log2(n). The runtime
will be equal to this bound if n, (n-1)/2, ((n-1)/2)/2 etc. are
all odd. Otherwise it will be lower than this bound.

Or am I missing something?

Steve
Nov 7 '06 #8
I wrote,
>That doesn't seem right. Any pair of consecutive calls
beginning with power(n) must reduce its argument by at least a factor
of 2 since the only possibilities are:

n = 2*k (n even) which results in a call to power(k)

n = 2*k+1 (n odd) which results in a call to power(2*k), then a
call to power(k)

Therefore the runtime is bounded by 2 * log2(n). The runtime
will be equal to this bound if n, (n-1)/2, ((n-1)/2)/2 etc. are
all odd. Otherwise it will be lower than this bound.
Oops, that should be: n, (n-1)/2, ((n-1)/2 - 1)/2 etc.

S.
Nov 7 '06 #9
Steve Pope wrote:
Stuart Redmann <De*******@web.dewrote:

>>>><ma****@gmx.atwrote:

>
Expand|Select|Wrap|Line Numbers
  1. >double power(double x, int n)
  2. >{
  3. >if (n < 0)
  4.  return 1/power(x, -n);
  5. >else if (n == 0)
  6.  return 1.0;
  7. >
  8. if (0 == n%2)
  9. {
  10. >       double y = power(x,n/2);
  11. >       return y*y;
  12. }
  13. else
  14. >       return x*power(x,n-1);
  15. >}
  16. >

>>3. If you put an exponent of the form 2^n - 1 into your power function,
you still end up with a complexity of O(n), since the step
return x*power(x,n-1) will be called 1/2 * 2^n - 1 times.


That doesn't seem right. Any pair of consecutive calls
beginning with power(n) must reduce its argument by at least a factor
of 2 since the only possibilities are:

n = 2*k (n even) which results in a call to power(k)

n = 2*k+1 (n odd) which results in a call to power(2*k), then a
call to power(k)

Therefore the runtime is bounded by 2 * log2(n). The runtime
will be equal to this bound if n, (n-1)/2, ((n-1)/2)/2 etc. are
all odd. Otherwise it will be lower than this bound.

Or am I missing something?
Nope. Obviously I did (*shame*). I didn't read your code properly (no
comments, BTW). Of course, the computational complexity you have given
is right. Using this algorithm is certainly even the most elegant
solution since recursion is the shortest way to implement it.

Sorry for giving bad advice (at least point 3 ;)

Stuart
Nov 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paul D. Sullivan | last post by:
I've been trying to find out how to increase the font size in the text entry fields in Invision Power Board 1.3 for a while. The part where you enter your post and do the quick reply and all that...
6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
9
by: Aaron Gallimore | last post by:
Hi, Pretty simple one I think...Is there a "power of" or squared function in C++. This is what i'm trying to achieve. (array-array)*2 this doesnt work with minus numbers so i need a square...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
3
by: Florent | last post by:
Hi all, Like you may all have seen, Power AD 1.3 had this bug at start : "PowerAD.exe - Common Language Runtime Debug Service ID processus=0x96c (2412), ID thread=0x218 (536). Click OK to...
5
by: Gus007 | last post by:
Hi all, Need the community great support once more. :) I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a...
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
2
by: =?Utf-8?B?VGVycmVsbA==?= | last post by:
Hi I purchased a brand new computer 2 years ago. About the last 2 months I noticed my blue power light wasn't as bright as it use to be, in fact you can barely see it. I also noticed my fan was...
8
by: skanemupp | last post by:
how do i solve power(5,1.3)? def power(nbr, po): if po==0: return 1 if po>0: return nbr*power(nbr, po-1) if po<0: return 1/power(nbr, -1*po)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.