Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum large numbers?

Tags:

c

c89

largenumber

I am trying to calculate 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + ... + 1 * 2 * ... * n where n is the user input. It works for values of n up to 12. I want to calculate the sum for n = 13, n = 14 and n = 15. How do I do that in C89? As I know, I can use unsigned long long int only in C99 or C11.

  1. Input 13, result 2455009817, expected 6749977113
  2. Input 14, result 3733955097, expected 93928268313
  3. Input 15, result 1443297817, expected 1401602636313

My code:

#include <stdio.h> #include <stdlib.h> int main() {     unsigned long int n;     unsigned long int P = 1;     int i;     unsigned long int sum = 0;     scanf("%lu", &n);     for(i = 1; i <= n; i++)     {         P *= i;         sum += P;     }     printf("%lu", sum);     return 0; } 
like image 966
Timʘtei Avatar asked May 30 '17 10:05

Timʘtei


People also ask

How do you sum big numbers in C++?

Step 1: loop from n to 0. Step 1.1: intSum = number1[i] + number2[i] Step 1.2: carry = intSum/10. Sum += intSum Step 2: sum += carry. Step 3: return sum.

How do you add two large numbers in Java?

math. BigInteger. add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result.


2 Answers

In practice, you want some arbitrary precision arithmetic (a.k.a. bigint or bignum) library. My recommendation is GMPlib but there are other ones.

Don't try to code your own bignum library. Efficient & clever algorithms exist, but they are unintuitive and difficult to grasp (you can find entire books devoted to that question). In addition, existing libraries like GMPlib are taking advantage of specific machine instructions (e.g. ADC -add with carry) that a standard C compiler won't emit (from pure C code).

If this is a homework and you are not allowed to use external code, consider for example representing a number in base or radix 1000000000 (one billion) and code yourself the operations in a very naive way, similar to what you have learned as a kid. But be aware that more efficient algorithms exist (and that real bignum libraries are using them).

A number could be represented in base 1000000000 by having an array of unsigned, each being a "digit" of base 1000000000. So you need to manage arrays (probably heap allocated, using malloc) and their length.

like image 52
Basile Starynkevitch Avatar answered Sep 19 '22 21:09

Basile Starynkevitch


You could use a double, especially if your platform uses IEEE754.

Such a double gives you 53 bits of precision, which means integers are exact up to the 53rd power of 2. That's good enough for this case.

If your platform doesn't use IEEE754 then consult the documentation on the floating point scheme adopted. It might be adequate.

like image 23
Bathsheba Avatar answered Sep 19 '22 21:09

Bathsheba