Handling Large Numbers in C: A Tutorial to Find Large Exponentiation of a Number
Browse articles:
Auto Beauty Business Culture Dieting DIY Events Fashion Finance Food Freelancing Gardening Health Hobbies Home Internet Jobs Law Local Media Men's Health Mobile Nutrition Parenting Pets Pregnancy Products Psychology Real Estate Relationships Science Seniors Sports Technology Travel Wellness Women's Health
Browse companies:
Automotive Crafts, Hobbies & Gifts Department Stores Electronics & Wearables Fashion Food & Drink Health & Beauty Home & Garden Online Services & Software Sports & Outdoors Subscription Boxes Toys, Kids & Baby Travel & Events

Handling Large Numbers in C: A Tutorial to Find Large Exponentiation of a Number

Handling large numbers is an important issue in computer programming. Very often in number theory we need to deal with numbers of astronomical size and we are forced to rely upon computers for manipulation of such numbers. This article describes the method of handling very large numbers in C by showing how to find large exponentiation of numbers.

Exponentiation is one of the few mathematical operations which increase the number of digits extremely fast. In number theory we often need to find large exponentiation of a number, e.g. finding 25th Mersenne prime which involves calculation of 2^21701 having 6533 digits. Hence for the obvious reason we rely upon computers for calculating such big numbers. But it is not easy to handle numbers having such a huge magnitude since all standard programming languages use 32 bit (or 64 bit at maximum) arithmetic for integer operations and thus the maximum number of integers that can be stored in a computer memory is 2^32 (or 2^64) which is a mere 10 digit number. Floating point arithmetic, which uses 64 bit arithmetic, has a maximum precision of only 17 digits. In this article I will discuss the method of handling large numbers in “C” by showing how to find large exponentiation of a very large number.

The Method

The basic idea behind storing a very large number is to use arrays and manipulation of them is to be done using a base higher than base 10, e.g. 16, 64 or 100000. Higher is the value of base lesser is the number of operations for a particular calculation. Though we can use any positive integer (limited by the word length of a particular data type) as the base, it is convenient to use a base which is a multiple of 10. The main advantage of using such base is that we do not need to back convert the number into base 10 which is the base of the output we want. It is because the face value of a number in such base is same as its decimal value. Thus our strategy of finding exponentiation will be to store the input in an array of appropriate length and then multiply it required number of times.

A “C” Program

Let us consider the C code provided below utilizing the method discussed above. It takes the number and the exponent from user as input and prints the result. The inputs must be positive, since what the sign will be after exponentiation of a negative number, can be easily determined by noting whether the exponent is even or odd. Again negative exponents are not considered here because large negative exponents often lead to fractions which are to be handled in a different manner.

Here I have used base 1000. The trivial cases are dealt separately using “if” statements. The function “digit_count” counts the number of digits present in an integer. The length of the array is obtained by calculating the number of digits in the final answer using the inbuilt function “ceil” which basically returns the smallest integer that is not less than its argument. The major step is to determine the multiplication which is done by calling function “multiply” shown below. While displaying the result it is taken care of to print some required leading zeros for some array elements.

A typical output will look like follows.

Few Limitations

In spite of the algorithm being perfectly fine the inputs are limited due to the fact that a computer uses 32 bit arithmetic so that each array element can store a maximum value max=2^32-1=4294967295. If “b” be the base used in the program then the input number “n” is limited by max/(b-1). Since we have used b=1000, the maximum value of n=4299266, an input value greater than this may give erroneous result. There is no limitation on the exponent except the value max=4294967295. However the program is fast enough to give the result within a fraction of second.

Thus we learned the method of handling large numbers in “C” and how to find large exponentiation of very large numbers.

Note: If you are unable to see the images properly, please zoom in the page to view clearly.

Need an answer?
Get insightful answers from community-recommended
experts
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (0)
ARTICLE DETAILS
RELATED ARTICLES
RELATED CATEGORIES
ARTICLE KEYWORDS
RECENT SEARCHES ON KNOJI SHOPPING