fuad_cs22

Large number factorial

Jan 13th, 2021
675
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. #define log(x) std::cout << x ;
  4. #define MAX 50000
  5.  
  6. int multiply(int x, int res[], int res_size);
  7.  
  8. uint64_t factorial(uint64_t a)
  9. {
  10.     uint64_t factorial = 1;
  11.     while (a)
  12.     {
  13.         factorial *= a--;
  14.     }
  15.     return factorial;
  16. }
  17.  
  18. void factorial(int n)
  19. {
  20.     int res[MAX];
  21.     res[0] = 1;
  22.     int res_size = 1;
  23.  
  24.     for (int x = 2; x <= n ; x++)
  25.     {
  26.         res_size = multiply(x, res, res_size);
  27.     }
  28.     for (int i = res_size - 1; i >= 0; i--)
  29.     {
  30.         log(res[i]);
  31.     }
  32. }
  33.  
  34. int multiply(int x, int res[], int res_size)
  35. {
  36.     int carry = 0;
  37.     for (int i = 0; i < res_size; i++)
  38.     {
  39.         int prod = res[i] * x + carry;
  40.         res[i] = prod % 10;
  41.         carry = prod / 10;
  42.  
  43.     }
  44.     while (carry)
  45.     {
  46.         res[res_size] = carry % 10;
  47.         carry = carry / 10;
  48.         res_size++;
  49.     }
  50.     return res_size;
  51. }
  52.  
  53. int main()
  54. {
  55.    
  56.     factorial(300);
  57.  
  58.     std::cin.get();
  59.  
  60. }
RAW Paste Data