-4

How to compute factorial of numbers like 300 as the output is not even in bound of unsigned long long int ?Please help.

#include<stdio.h>
#include<stdlib.h>


unsigned long long int factorial(int number) {
    unsigned long long int temp;

    if(number <= 1) return 1;

    temp = (number * factorial(number - 1));
    return temp;
}

int main(){
    int t,k,i,a[100001];
    unsigned long long int sum[100001];
    scanf("%d",&t);
    for(i=0;i<t;i++){
       scanf("%d",&a[i]);

    }
    for(k=0;k<t;k++){
             sum[k]=0;
    for(i=0;i<=a[k];i++){
        sum[k] += ((factorial(a[k])/(factorial(i)*factorial(a[k]-i)))%3);
         //printf("%d\n",sum[k]);
    }}
    for(i=0;i<t;i++){
        printf("%llu\n",sum[i]);
    }
    return 0;

}

I tried this but it halt at only 60!.

Tushar Gupta
  • 1,603
  • 13
  • 20

1 Answers1

1

You can try this for Factorials of large numbers :

#include<iostream>
#include<cstring>

int max = 5000;

void display(int arr[]){
    int ctr = 0;
    for (int i=0; i<max; i++){
        if (!ctr && arr[i])         ctr = 1;
        if(ctr)
            std::cout<<arr[i];
    }
}


void factorial(int arr[], int n){
    if (!n) return;
    int carry = 0;
    for (int i=max-1; i>=0; --i){
        arr[i] = (arr[i] * n) + carry;
        carry = arr[i]/10;
        arr[i] %= 10;
    }
    factorial(arr,n-1);
}

int main(){
    int *arr = new int[max];
    std::memset(arr,0,max*sizeof(int));
    arr[max-1] = 1;
    int num;
    std::cout<<"Enter the number: ";
    std::cin>>num;
    std::cout<<"factorial of "<<num<<"is :\n";
    factorial(arr,num);
    display(arr);
    delete[] arr;
    return 0;
}
Sakib Ahammed
  • 2,452
  • 2
  • 25
  • 29