john said:
> Richard Heathfield wrote:
>>
>> And that's it (except that I haven't tested it as such). Not sure why
>> you need powers.
>
>
> I checked this, and it does not work for negatives.
Well, what do you want it to do for negatives? Does -1234 become -4321 or
4321- ? If the first, the fix is obvious - record the sign, take the
absolute value, and adjust at the end. If the second, use a string and
reverse that instead (which is a lot easier).
> I need the powers because i store the reversed digits in an array, and
> then want to store them in a variable.
>
> So the reversed digits of 1234 are 4321 and it can be stored in a
> variable in the style:
>
> 4* 10^3+ 3* 10^2+ 2*10^1+ 1* 10^0.
Well, integer powers are easy enough. Just remember that
x^1 = x
x^(a + b) = x^a * x^b
You can use these rules to calculate x^n trivially, by observing that if n
is 1, then you're done, otherwise if n is odd, you can simplify the
problem to x * x^(n-1), otherwise you can simplify to x^(n/2) * x^(n/2).
Combine these rules recursively as shown elsethread (by another
respondent), and you're done.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www.
+rjh@[EMAIL PROTECTED]
users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


|