Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > factorial and exponent

Reply
Thread Tools

factorial and exponent

 
 
Thomas
Guest
Posts: n/a
 
      06-16-2007
I want to calculate the value of 126 raise to the power 126 in turbo
C.
I've checked it with unsigned long int but it doesn't help.
So how could one calculate the value of such big numbers?
What's the technique?

 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      06-16-2007

"Thomas" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
>I want to calculate the value of 126 raise to the power 126 in turbo
> C.
> I've checked it with unsigned long int but it doesn't help.
> So how could one calculate the value of such big numbers?
> What's the technique?


1. Use another programming language, or
2. find a bignum library, or
3. don't compute it. Compute its base-10 log. The integer part will
be the exponent, and from the fractional part you can find out the
mantissa.

<ot> log10(126**126) = 126 * log10(126) </ot>
printf("%fe%d", pow(10, x - floor(x)), (int)floor(x));

where x is 126 * log10(126).

HTH.


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-16-2007
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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-16-2007
Thomas wrote:
>
> I want to calculate the value of 126 raise to the power 126 in
> turbo C. I've checked it with unsigned long int but it doesn't
> help. So how could one calculate the value of such big numbers?
> What's the technique?


First, decide what holds the answer. You will need in the order of
1000 bits. Probably at least two of them.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      06-17-2007
On Jun 16, 5:02 pm, Thomas <(E-Mail Removed)> wrote:
> I want to calculate the value of 126 raise to the power 126 in turbo
> C.
> I've checked it with unsigned long int but it doesn't help.
> So how could one calculate the value of such big numbers?
> What's the technique?


Use GMP library found in http://gmplib.org/
It will enable you to do "Arithmetic without Limitations" !!

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-17-2007
BiGYaN said:

> On Jun 16, 5:02 pm, Thomas <(E-Mail Removed)> wrote:
>> I want to calculate the value of 126 raise to the power 126 in turbo
>> C.
>> I've checked it with unsigned long int but it doesn't help.
>> So how could one calculate the value of such big numbers?
>> What's the technique?

>
> Use GMP library found in http://gmplib.org/
> It will enable you to do "Arithmetic without Limitations" !!


Nonsense.

Consider an integer greater than or equal to 2. Call it A. Consider
another integer greater than or equal to 2. Call it B.

Raise A to the power B, storing the result in A. Now raise B to the
power A, storing the result in B. If you repeat this often enough, you
*will* hit a limit, no matter what numerical library you use.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      06-17-2007

"Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> BiGYaN said:
>
>> On Jun 16, 5:02 pm, Thomas <(E-Mail Removed)> wrote:
>>> I want to calculate the value of 126 raise to the power 126 in turbo
>>> C.
>>> I've checked it with unsigned long int but it doesn't help.
>>> So how could one calculate the value of such big numbers?
>>> What's the technique?

>>
>> Use GMP library found in http://gmplib.org/
>> It will enable you to do "Arithmetic without Limitations" !!

>
> Nonsense.
>
> Consider an integer greater than or equal to 2. Call it A. Consider
> another integer greater than or equal to 2. Call it B.
>
> Raise A to the power B, storing the result in A. Now raise B to the
> power A, storing the result in B. If you repeat this often enough, you
> *will* hit a limit, no matter what numerical library you use.


But it is a limit of your computer, not of the library itself.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      06-17-2007
Army1987 wrote, On 17/06/07 09:48:
> "Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>> BiGYaN said:
>>
>>> On Jun 16, 5:02 pm, Thomas <(E-Mail Removed)> wrote:
>>>> I want to calculate the value of 126 raise to the power 126 in turbo
>>>> C.
>>>> I've checked it with unsigned long int but it doesn't help.
>>>> So how could one calculate the value of such big numbers?
>>>> What's the technique?
>>> Use GMP library found in http://gmplib.org/
>>> It will enable you to do "Arithmetic without Limitations" !!

>> Nonsense.
>>
>> Consider an integer greater than or equal to 2. Call it A. Consider
>> another integer greater than or equal to 2. Call it B.
>>
>> Raise A to the power B, storing the result in A. Now raise B to the
>> power A, storing the result in B. If you repeat this often enough, you
>> *will* hit a limit, no matter what numerical library you use.

>
> But it is a limit of your computer, not of the library itself.


If it uses space allocated with malloc/realloc, then the library (rather
than the computer) has a limit because even with an infinite computer
size_t and pointers are of defined finite size, so you can only have a
block of known finite size and you can only chain a finite number of
such blocks together with pointers.

Of course, this applies to all libraries written in C.

It is also very important for people learning to be programmers (or who
already are programmers) to understand that in the real world resources
are always limited, so there is no such thing as "without limitations".
--
Flash Gordon
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-17-2007
Army1987 said:
> "Richard Heathfield" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>> BiGYaN said:

<snip>
>>>
>>> Use GMP library found in http://gmplib.org/
>>> It will enable you to do "Arithmetic without Limitations" !!

>>
>> Nonsense.
>>
>> Consider an integer greater than or equal to 2. Call it A. Consider
>> another integer greater than or equal to 2. Call it B.
>>
>> Raise A to the power B, storing the result in A. Now raise B to the
>> power A, storing the result in B. If you repeat this often enough,
>> you *will* hit a limit, no matter what numerical library you use.

>
> But it is a limit of your computer, not of the library itself.


Nevertheless, it is a limit, and therefore the library *cannot* 'enable
you to do "Arithmetic without Limitations"', and therefore BiGYaN's
statement is nonsense.

Incidentally, you've just emerged from a 30-day spell in my sin bin. I
hope I won't have to chuck you back in there.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      06-17-2007
On Jun 17, 12:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> BiGYaN said:
>
> > On Jun 16, 5:02 pm, Thomas <(E-Mail Removed)> wrote:
> >> I want to calculate the value of 126 raise to the power 126 in turbo
> >> C.
> >> I've checked it with unsigned long int but it doesn't help.
> >> So how could one calculate the value of such big numbers?
> >> What's the technique?

>
> > Use GMP library found inhttp://gmplib.org/
> > It will enable you to do "Arithmetic without Limitations" !!

>
> Nonsense.
>
> Consider an integer greater than or equal to 2. Call it A. Consider
> another integer greater than or equal to 2. Call it B.
>
> Raise A to the power B, storing the result in A. Now raise B to the
> power A, storing the result in B. If you repeat this often enough, you
> *will* hit a limit, no matter what numerical library you use.


"Arithmetic without Limitations" is sort of a slogan for GMP (http://
gmplib.org/). That's why I just put it in quotes.

The case that you are talking about does not show the limitation of
the numerical library. It's a limit of your computer. Besides, for all
*practical purposes* you won't hit this limit in a modern computer.
Like I'm quite sure that nobody will actually need all the digits of
126^126 for any *practical* job.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT / XPATH 2.0 split float to exponent / mantissa and find nearestengineering format RolfK XML 1 01-20-2009 04:00 PM
some problem with the min exponent of float and double in <limits> header? Wayne Shu C++ 1 03-18-2007 02:16 PM
iostreams - removing leading zeros and controlling exponent field width Anjo Gasa C++ 0 01-30-2007 09:05 PM
How to find exponent and mantissa from a Q Format sankar C Programming 5 11-24-2005 03:34 PM
Is there any library which defines factorial and binomial coefficient? PengYu.UT@gmail.com C++ 2 05-08-2005 10:31 PM



Advertisments