 |
19-03-2008, 01:26 AM
|
#1 (permalink)
|
|
Who stole my Alpaca!
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
|
Need help with using GMP BigNum Library
Hi all,
I have been working on Project Euler questions and I'm trying to understand how to use this GMP Bignum library and I've read through a bit of the documentation but this is way way more complex than what I expected lol. I'd appreciate if soeone could either post a few SIMPLE examples using it. If you could attach the code, that woulds be great. Please dont post the ones in the demo folder of the gmp library cause I can barely understand them at all lol.
Thanks
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
19-03-2008, 05:41 PM
|
#2 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Need help with using GMP BigNum Library
Here's a simple code which adds and subtracts 2 integers among 3:
Code:
#include <gmp.h>
int main(void)
{
// We declare a few BigNum Integer
mpz_t I, K, M;
// Some BigNum Floats (Not gonna use them)
// Just demo
mpf_t U,J,N;
int n;
// Initialize the Integer I as 0
// set a value for the rest two (K and M)
// We need to initialize ANY BigNum value
// Its a must
mpz_init(I); // Initialized with 0
mpz_init(K);
mpz_set_si(K, -3000000);
// si means Signed Integer
// set_* is used to set values
mpz_init_set_ui(M, 999999999);
// Initialized and set via Unsigned at a time
// Similarly one can convert via Double, Strings, etc
// and even set via other mpz_t values
// Also use xxx_clear() to clear
// Add M, K and store in I
mpz_add(I,M,K);
// Subtraction, another example
mpz_sub(M,K,I);
gmp_printf("I = %Zd, K = %Zd, M = %Zd \n", I, K, M);
// Refer the manual for other _functions_ relating to integers
return 0;
}
/*
Note on GMP in general
There are 3 basic types of Numbers in GMP
Integer - Represented as "Z"
Float - Represented as "F"
Rational - Represented as "Q"
There are some more types, such as Limbs and so,
but I don't know much about them.
So in gmp_printf(), the strings obviously are:
%Z<format> for Integer
%F<format> for Float
%Q<format> for Rational
Where <format> can be any C-allowed printf() type such as:
d - Integer
f - Float
x - Hex
gmp_scanf()'s the same
P.s. Heard that stuff for O/P and I/P are easier in C++ but,
I prefer C
*/
See it in some editor which highlights comments out at least.
__________________
Harsh J
www.harshj.com
|
|
|
19-03-2008, 05:49 PM
|
#3 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: Need help with using GMP BigNum Library
Are you sure you checked the manual? It seemed pretty simple to me. Just read the part on initialization, then all the arithmetic functions, then the I/O. That should be all you need.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
19-03-2008, 05:52 PM
|
#4 (permalink)
|
|
GTK+ programmer
Join Date: Jun 2007
Posts: 121
|
Re: Need help with using GMP BigNum Library
Even I had decided to use BigNum library for some of ProjectEuler's problems. But due to its complexity, learning python turned out to be a simpler option(learning loops and functions hardly takes 10 min)
|
|
|
19-03-2008, 07:14 PM
|
#5 (permalink)
|
|
Who stole my Alpaca!
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
|
Re: Need help with using GMP BigNum Library
QwertM : Thank you for the guidance  That explains alot.
Quote:
|
Are you sure you checked the manual? It seemed pretty simple to me. Just read the part on initialization, then all the arithmetic functions, then the I/O. That should be all you need.
|
Hmm Maybe Im looking at the wrng manual then. But I felt that it was way too complex  .
Quote:
|
Even I had decided to use BigNum library for some of ProjectEuler's problems. But due to its complexity, learning python turned out to be a simpler option(learning loops and functions hardly takes 10 min)
|
yeah I've beent empted to change to Python but I'd like to make sure I atleast try in C befoer I give up.
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
|
|
|
20-03-2008, 05:55 AM
|
#6 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Need help with using GMP BigNum Library
__________________
Harsh J
www.harshj.com
|
|
|
20-03-2008, 01:20 PM
|
#7 (permalink)
|
|
Who stole my Alpaca!
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
|
Re: Need help with using GMP BigNum Library
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
|
|
|
20-03-2008, 06:38 PM
|
#8 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: Need help with using GMP BigNum Library
The entire python standard library _is_ written in C. So if you maximize your usage of builtins, you'll be able to get C's speed, and write in python, which is heavenly.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|