Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Ansi -iso > Re: Turbo Pasca...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 16 Topic 199 of 215
Post > Topic >>

Re: Turbo Pascal question

by Richard Heathfield <rjh@[EMAIL PROTECTED] > Dec 22, 2007 at 11:43 AM

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
 




 16 Posts in Topic:
Turbo Pascal question
john <john@[EMAIL PROT  2007-12-20 21:09:11 
Re: Turbo Pascal question
Richard Heathfield <rj  2007-12-20 19:29:28 
Re: Turbo Pascal question
Dr J R Stockton <jrs@[  2007-12-21 13:44:12 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-22 01:15:05 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-22 01:38:43 
Re: Turbo Pascal question
Richard Heathfield <rj  2007-12-22 00:58:29 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-22 13:18:49 
Re: Turbo Pascal question
Richard Heathfield <rj  2007-12-22 11:43:12 
Re: Turbo Pascal question
"Jochen" <jo  2007-12-22 22:42:57 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-23 01:33:35 
Re: Turbo Pascal question
"Jochen" <jo  2007-12-23 05:56:25 
Re: Turbo Pascal question
scott moore <nospam@[E  2007-12-23 10:33:04 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-24 00:59:51 
Re: Turbo Pascal question
Dr J R Stockton <jrs@[  2007-12-22 17:51:06 
Re: Turbo Pascal question
john <john@[EMAIL PROT  2007-12-22 20:52:31 
Re: Turbo Pascal question
Dr J R Stockton <jrs@[  2007-12-23 16:36:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 18:27:21 CDT 2008.