BIG numbers in C

I enjoy learning about different programming languages. One of my favourite resources for finding programming challenges is the Project Euler website. There are almost 500 problems building up in difficulty. I have recently been learning to programme in C after originally learning Python. I usually a sizable chunk of time trying to work out how to do things in C that would be trivial in Python. One huge difference is in the C's handling of BIG integers. In Python there is no practical limit to the size of integers. However, in C we are limited by the operating system to either 232-1 for 32 bit systems or 264-1 for 64 bit systems. In many of the Project Euler problems we are required to find very big numbers. The solution is to use the gmp (Gnu multi-precision) library. Basically this library converts an integer into a string of single digit numbers. It contains functions for performing all the basic arithmetical operations on these strings. Here is a step by step guide on how to get started. All commands will be based on a Ubuntu operating system.

First install the library, run this command in the terminal with this command:

sudo apt-get install libgmp3-dev

Copy this code into a file named big_numbers.c

compile with this command:

gcc big_numbers.c -o big_numbers -lgmp

and run with this command to test multiplying some big numbers together

./big_numbers 999999999999 999999999999

The following links give some further information:

Sriram Sankaranarayanan's tutorial

Beej's Bit Bucket

Stackoverflow discussion

Comments