![]() |
|
#1
|
|||
|
|||
Recursive power(x,n) functionhi every one
i'm new to C++ i get a assignment that i need to wirte a recursive function for exponential i wrote a silpme one, and it works, then the teacher ask me to modify it she wrote this: "The "simple" algorithm does n-1 multiplications but it is possible to compute x^n for n=16 in 4 multiplications. Modify your program to output also the value of counter. For x=2.0 and n=16, the output would be x=2 n=16 x^n=65536 multiplications=4 Hint: One way to calculate x8 is to calculate x · x · x · x · x · x · x · x which takes 7 multiplications. But x · x · x · x · x · x · x · x = (x · x · x · x)^2 = ((x · x)^2)^2 which can be computed with 3 multiplications." but i try a so many time, still dont get it. i need some help for this. here is my code The "simple" algorithm she said.. CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: recursive power(x,n) functioni can't edit my post, so i reply my new update
i can now do the 2^8 in 3 muliti. but still can't do 2^16 in 4 muliti.. here is the new code CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
Re: Recursive power(x,n) functionHere's a hint (I'm not going to give away the answer since it's a school assignment).
(Notation: By x^y, I mean x to the y power, not the bitwise XOR operator). CPP / C++ / C Code:
__________________
www.blake-foster.com |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 65 | 19-Aug-2009 08:50 |
| Flex and bison coding | lucky88star | C++ Forum | 5 | 24-Dec-2007 12:57 |
| Need Help with input files. | Efferus | C++ Forum | 2 | 24-Nov-2007 17:19 |
| recursive function for perfect numbers? | cancan | C Programming Language | 6 | 19-Nov-2007 12:46 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The