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.
44329076602207821491972574571700100562486647339617150064334557177890\
43517106373872170818953941792055669609014893218047089803712563472169\
06583373889953014265747680923405829337012685381706863104615274196776\
3913240019546541793769190722594113575550312228000452759781376
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.