473,618 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

power of a large int and a modular airthmetic problems.

Hi,

I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.

My power function is as follows: for(i=0; i<5000; i++) result *= 2;

Are there any ways of storing this large a number while/when
calculating this number to the power of a large number?

Assume that I could get the large number stored correctly. I will
have to carry out the number mod p, which is result%p.

The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?

Thanks,
Nov 13 '05 #1
18 5625
On Wed, 27 Aug 2003 00:49:21 -0700, Zero wrote:
Hi,

I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.

My power function is as follows: for(i=0; i<5000; i++) result *= 2;

Are there any ways of storing this large a number while/when
calculating this number to the power of a large number?

Assume that I could get the large number stored correctly. I will
have to carry out the number mod p, which is result%p.

The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?


You'll have to make your own datatype, maybe something like:

typedef struct {
size_t num_bytes;
unsigned char *bytes;
} huge_number;

And functions for any kind of arithmetic operation you want to do on the
numbers.

void huge_times_2(hu ge_number *h);
huge_number mod(huge_number *h, int m);

// -1: a<b 0: a==b 1: a>b
int compare(huge_nu mber *a, int b);
void sub(huge_number *h, int s);

huge_number *mod(huge_numbe r *h, int m)
{
while ( compare(huge_nu mber,m) >= 0) {
sub(huge_number ,m);
}

return h;
}


This is probably not the best design but you get the general idea: define
your own type to represent the numbers, and operations on that type. Since
you asked about modulo I included an example, the algoritm is basically

substract m from the target until you get a result that is less than m,
then return it.
hth
NPV
Nov 13 '05 #2
Ema

"Zero" <sb********@yah oo.com> ha scritto nel messaggio
news:73******** *************** ***@posting.goo gle.com...
Hi,

I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.

My power function is as follows: for(i=0; i<5000; i++) result *= 2;

Are there any ways of storing this large a number while/when
calculating this number to the power of a large number?

Assume that I could get the large number stored correctly. I will
have to carry out the number mod p, which is result%p.

The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?

Thanks,


Don't know if it could be of help:

I don't remember if it true the following:

((A*B) % p) = ((A % p) * (B % p)) % p

but if it's true then

2^k % p =
(2 * 2^(k-1)) % p =
((2 % p) * (2^(k-1) % p)) % p =
((2 % p) * (((2 % p) * (2^(k-2) % p)) % p)) % p =
....
if (p==2) then the result is 0
if (p > 2)
((2 % p) * (((2 % p) * (2^(k-2) % p)) % p)) % p
becomes
(2 * ((2 * (2^(k-2) % p)) % p)) % p

and you can try to find an algorithm to calculate this without falling in
overflow.

Bye
Ema
Nov 13 '05 #3
Zero wrote:
Hi,

I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.


2 to tne nth power requires n+1 bits; a type with 64 bits does not have
5001 bits, does it?


--
Martin Ambuhl

Nov 13 '05 #4
Use lcc-win32

#include <stdio.h>
#include <qfloat.h>
int main(void)
{
qfloat a;

printf("2^5000= \n");
a = pow(2q,5000q);
printf("%.60qe\ n",a);
return 0;
}

Output:
---------

2^5000=
1.4124670321394 260368352096670 161473336688961 751845411168136 9e+1505

But beware: those are floating point numbers, not integers.
"Zero" <sb********@yah oo.com> wrote in message
news:73******** *************** ***@posting.goo gle.com...
Hi,

I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.

My power function is as follows: for(i=0; i<5000; i++) result *= 2;

Are there any ways of storing this large a number while/when
calculating this number to the power of a large number?

Assume that I could get the large number stored correctly. I will
have to carry out the number mod p, which is result%p.

The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?

Thanks,

Nov 13 '05 #5
> "Zero" <sb********@yah oo.com> wrote in message
news:73******** *************** ***@posting.goo gle.com... On Wed, 27 Aug 2003 15:58:38 +0200, jacob navia wrote:
The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?

But beware: those are floating point numbers, not integers.


So how do you do modulo on that floating point number then?
NPV
Nov 13 '05 #6
Paul Hsieh wrote:
If p is small, there is a standard way of doing this. You don't need bignum
libraries, and you don't need to store the big number. Ema's answer is the
closest I see posted so far, but this is really a sci.math question, not a
comp.lang.c question:

unsigned int i, x, y;
x = 2;
y = 1;
for (i=5000; i >= 0; i >> 2) {
Two errors in one line:
- i will always be >= 0
- i >> 2 is a no-op
if (i & 1) y = (y * x) % p;
What is p?
x *= x;
This will overflow _very_ soon.
}
return y;


You should have answered this in sci.math, as you suggested. :-)

Jirka

Nov 13 '05 #7
Paul Hsieh wrote:

If p is small, there is a standard way of doing this. You don't need bignum
libraries, and you don't need to store the big number. Ema's answer is the
closest I see posted so far, but this is really a sci.math question, not a
comp.lang.c question:

unsigned int i, x, y;
x = 2;
y = 1;
for (i=5000; i >= 0; i >> 2) {
if (i & 1) y = (y * x) % p;
x *= x;
}
return y;


Infinite loop as written; `i >> 2' should probably
be `i >>= 1'.

Unfortunately, even with the correction this will
produce the answer zero on any reasonable machine because
the `x *= x' will eventually exceed `UINT_MAX' and will
store the value zero into `x'. (The "mathematic al" value
of `x' would be two-to-the-large, and this value is exactly
divisible by `UINT_MAX+1' == two-to-the-smaller.)

--
Er*********@sun .com
Nov 13 '05 #8

"Nils Petter Vaskinn" <no@spam.for.me .invalid> wrote in message
news:pa******** *************** *****@spam.for. me.invalid...
"Zero" <sb********@yah oo.com> wrote in message
news:73******** *************** ***@posting.goo gle.com...

On Wed, 27 Aug 2003 15:58:38 +0200, jacob navia wrote:
The problem is if I stored the large number using linked list (which I
don't know how), for example, how would I carry out the mod p?

But beware: those are floating point numbers, not integers.


So how do you do modulo on that floating point number then?
NPV


Without resorting to bignums we can still do:

#include <qfloat.h>
#include <stdio.h>
int main(void)
{

printf("(2^5000 ) %%10000=%qf\n", qfmod(pow(2q,50 00q),10000q));
return 0;
}

and it prints
(2^5000) %10000=9376
as it should, the same number I obtain with the bignums package.
Nov 13 '05 #9
>I am calculating an integer to the pwer of a large integer, e.g.
2^5000.

It turns out that the result I always get is zero. I am sure that the
result is too large to store in such type as u_int64_t and long int
etc.

My power function is as follows: for(i=0; i<5000; i++) result *= 2;
You need an integer with at least 5001 bits; most implementations
don't have such a thing. Floating point numbers often don't have
enough range in the exponent to represent this, either. A standard
IEEE double won't.
Are there any ways of storing this large a number while/when
calculating this number to the power of a large number?

Assume that I could get the large number stored correctly. I will
have to carry out the number mod p, which is result%p.


It is often simpler to calculate result%p by a means other than
calculating result, then doing the %p operation.

For example:
c = a * b
(c % p) = ((a % p) * (b % p) ) % p

If p fits in a long, so do a%p and b%p. (a%p)*(b%p) will fit in
something twice the size of a long. You can do exponentiation by
repeated multiplication.

If you really need to compute with large numbers, there are a
number of "bignum" packages which can deal with large or
arbitrarily large numbers.

Gordon L. Burditt
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
6359
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
5
2261
by: Tim | last post by:
Dear All, I have been working with VB.NET for the last 5 months or so as a solo developer for a small business. I have already started developing the application but have hit a snag with the referencing of one project within another. I would like to develop the solution using a modular approach such that the solution will be made up of several smaller projects being referenced by a main application project. Some of these smaller
6
3996
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
0
1591
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this very auspicious occasion, Thai people will join hands with
0
1593
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this very auspicious occasion, Thai people will join hands with
4
2301
by: Nick Goloborodko | last post by:
Hi, I'm in the process of conceptualizing a new ASP.NET application. I'm a relative newbie in ASP.NET / .NET in general, so any comments will be greatly appreciated. Basically i need to make my application as modular as possible, something along the lines of Mambo (PHP CMS System) Drupal (another PHP CMS) or something similar to these. What I'm thinking is to provide the following structure: core of the application includes essential...
26
4832
by: I_AM_DON_AND_YOU? | last post by:
This is the scenario: I have a VB.Net project comprising of a few Forms. On Form1 I have more than 20 buttons. There is a very lenghty code written in click event of each and every button. Right now I haven't used any sub procedure. I mean to say I am writing the code directly in the click event. So it's become very lengthy and therefore to figure out some problem or make any changes I have to scroll at lot. Also in addition to click...
2
3476
by: Canice | last post by:
I'm working on a web application where 90% of it is common 'product' code an the other 10% is customer specific. I want some method of separating the customer specific presentation, business and data access layers from the product code as I don't the main product code to be bloated with customer specific code. Ideally I'd like to have one solution for the product and one for each customer. However I haven't found a way to separate the...
12
4886
by: calimero22 | last post by:
Hi I'musing the GMP Libraries Mathematics. I need to calculate 12345678987654321.1234567876554456 ^ 9876543212344556676.676767676 How can i do ? The two variables are type MPF. Thanks
0
8595
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8455
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5552
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.