简体   繁体   中英

How to sum large numbers?

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;
}

In practice, you want some arbitrary precision arithmetic (aka 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 (eg 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.

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.

A simple approach when you're just over the limit of MaxInt, is to do the computations modulo 10^n for a suitable n and you do the same computation as floating point computation but where you divide everything by 10^r.The former result will give you the first n digits while the latter result will give you the last digits of the answer with the first r digits removed. Then the last few digits here will be inaccurate due to roundoff errors, so you should choose ra bit smaller than n. In this case taking n = 9 and r = 5 will work well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM