Big integer does not work

4

My code looks like this:

#include <stdio.h>

int main()
{
    unsigned long int L, N;
    scanf("%lu%lu", &L, &N);

    printf("%lu\n", (L-(N-1))*(L-(N-1)) + (N-1));

    return 0;
}

When the test case has low numbers, the program works. However, when the numbers are beyond the capacity of unsigned long int , with entries like 1,000,000 and 9 (the correct answer would be 999984000072) the program gives the wrong answer.

How can I operate with larger integers?

    
asked by anonymous 11.02.2016 / 22:50

2 answers

8

If you have a C99 implementation, you can try unsigned long long . This type has to exist, but may not be greater than unsigned long --- and therefore may not solve the problem.

Check with, for example sizeof (unsigned long) == sizeof (unsigned long long)

If you can use this type, remember to use "%llu" in both printfs and scanfs.

In this case, either you use a library for large numbers or you make your version of multiplication of numbers as you learned in elementary school (a very interesting project).

    
11.02.2016 / 23:00
6

Use the type unsigned long long

    
11.02.2016 / 23:01