The biggest number in C

2020-02-07 05:19发布

scanf("%d",&a);
for(b=1;b<=a;++b)
{
    c*=b;
}
printf("%lu",c);

I want to get the answer of 100!
factorial of 100. how can I get this? ( I wanna get a bigger range of numbers) Cant we have the number limit to infinity?

标签: c
3条回答
够拽才男人
2楼-- · 2020-02-07 05:55

Max integer range is, on just about every (modern) platform, 2^31 - 1 (although, by the standard, int is only required to be at least 16 bits). For your given platform, it'll be defined as INT_MAX in <limits.h>.

100! will obviously far exceed this. To calculate something this large in C, you'll need a big integer library, such as GMP.

Just as a cautionary note, if you decide to try and use a double (which can hold numbers of this size), you will get the wrong answer due to precision loss. This is easy to spot - on my machine, the last digits are 48, which is obviously nonsense: 100! must be divisible by 100, hence must have 00 as the last two digits.

查看更多
对你真心纯属浪费
3楼-- · 2020-02-07 05:58

For small numbers you better use unsigned long long than int. But still you have limit on the biggest number you can use for a. You could try double or float but you might get precession error.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-02-07 06:11
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#if __STDC_VERSION__>=199901L
#include <inttypes.h>
#else
#define PRIu16 "hu"
#endif

typedef struct _unums {
    size_t size;
    uint16_t *nums;//array 
} UNums;

void UNums_init(UNums *num, uint16_t n){
    num->nums = (uint16_t*)malloc(sizeof(uint16_t));
    num->nums[0] = n;
    num->size = 1;
}

void UNums_mul(UNums *num, uint16_t n){
    uint16_t carry = 0;
    size_t i;

    for(i=0;i<num->size;++i){
        uint32_t wk = n;
        wk = wk * num->nums[i] + carry;
        num->nums[i] = wk % 10000;
        carry = wk / 10000;
    }
    if(carry){
        num->size += 1;
        num->nums = (uint16_t*)realloc(num->nums, num->size * sizeof(uint16_t));
        num->nums[i] = carry;
    }
}

void UNums_print(UNums *num){
    size_t i = num->size;
    int w = 0;
    do{
        --i;
        printf("%0*" PRIu16, w, num->nums[i]);
        if(!w) w = 4;
    }while(i!=0);
}

void UNum_drop(UNums *num){
    free(num->nums);
    num->nums = NULL;
}

int main( void ){
    UNums n;
    uint16_t i;

    UNums_init(&n, 1);
    for(i=2;i<=100;++i)
        UNums_mul(&n, i);
    UNums_print(&n);//100!
    UNum_drop(&n);
    return 0;
}
查看更多
登录 后发表回答