In this article, I use ^ to represent "to the power of", rather than as

XOR.

Thomas said:

> I want to calculate the value of 126 raise to the power 126 in turbo

> C.
44329076602207821491972574571700100562486647339617 150064334557177890\

43517106373872170818953941792055669609014893218047 089803712563472169\

06583373889953014265747680923405829337012685381706 863104615274196776\

39132400195465417937691907225941135755503122280004 52759781376

> I've checked it with unsigned long int but it doesn't help.
Since the largest value you are likely to be able to store in an

unsigned long int in Turbo C is 4294967295, it's hardly surprising that

you can't represent 126^126 in that type.

> So how could one calculate the value of such big numbers?

> What's the technique?
How would you do it by hand?

To save you some work, you'd probably start off by observing that

126^126 =

(126^63)^2 =

((126^31)^2*126)^2 =

(((126^15)^2*126)^2*126)^2 =

((((126^7)^2*126)^2*126)^2*126)^2 =

(((((126^3)^2*126)^2*126)^2*126)^2*126)^2 =

((((((126^2)*126)^2*126)^2*126)^2*126)^2*126)^2

So if you can multiply a number by itself, and multiply a number by 126,

you can get your result quite quickly.

See Knuth's "The Art of Computer Programming", volume 2, for information

on how to multiply two arbitrarily large numbers.

Alternatively, learn how to use GNU's GMP package, or Miracl, both of

which have C bindings.

--

Richard Heathfield

"Usenet is a strange place" - dmr 29/7/1999

http://www.cpax.org.uk
email: rjh at the above domain, - www.