Advertisement
silentkiler029

500 Factorial Problem - solve by Ariful Islam Shanto

May 11th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. ///BISMILLAHIR-RAHMANIR-RAHIM
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define max 3000
  7. #define tot 1001
  8.  
  9. char facto[1001][max + 1];
  10. char serial_str[tot][5];
  11. char one[] = "0001";
  12.  
  13. void create_serial_string(int x)
  14. {
  15.     int i, j;
  16.     int sum = 0, carry = 0, temp;
  17.     for(i = 3; i >= 0; i--){
  18.         temp = serial_str[x - 1][i] + one[i] - '0' - '0' + carry;
  19.         sum = temp % 10;
  20.         carry = temp / 10;
  21.         serial_str[x][i] = sum + '0';
  22.     }
  23.     serial_str[x][4] = '\0';
  24. }
  25.  
  26. void serial_maker()
  27. {
  28.     int i, j;
  29.     for(i = 0; i < tot; i++){
  30.         for(j = 0; j < 4; j++){
  31.             serial_str[i][j] = '0';
  32.         }
  33.     }
  34.     for(i = 1; i < tot; i++){
  35.         create_serial_string(i);
  36.     }
  37. }
  38.  
  39.  
  40. void build_zero_string()
  41. {
  42.     int i, j;
  43.     for(i = 0; i <= 1000; i++){
  44.         for(j = 0; j < max; j++){
  45.             facto[i][j] = '0';
  46.         }
  47.     }
  48. }
  49.  
  50. void multiply_and_create_new_facto(int k)
  51. {
  52.     int product, carry = 0, character;
  53.     int i, j, l = 0;
  54.     for(j = 3, l = 1; j >= 0; j--, l++){
  55.         carry = 0;
  56.         for(i = max - 1; i >= 3; i--){
  57.             product = (int)(facto[k - 1][i] - '0') * (int)(serial_str[k][j] - '0') + carry + facto[k][i - l + 1] - '0';
  58.             carry = product / 10;
  59.             character = (product % 10) + '0';
  60.             facto[k][i - l + 1] = character;
  61.         }
  62.     }
  63.     facto[k][max] = '\0';
  64. }
  65.  
  66. void instruct_to_create_fibo()
  67. {
  68.     facto[0][max - 1] = '1', facto[0][max] = '\0';
  69.     facto[1][max - 1] = '1', facto[1][max] = '\0';
  70.     for(int i = 2; i <= 1000; i++){
  71.         multiply_and_create_new_facto(i);
  72.     }
  73. }
  74.  
  75. int main()
  76. {
  77.     serial_maker();
  78.     build_zero_string();
  79.     instruct_to_create_fibo();
  80.  
  81.     int n, i, j;
  82.  
  83.     while(scanf("%d", &n) == 1){
  84.         printf("%d!\n", n);
  85.         for(i = 0; i < max; i++){
  86.             if(facto[n][i] != '0'){
  87.                 for(j = i; j < max; j++){
  88.                     printf("%c", facto[n][j]);
  89.                 }
  90.                 printf("\n");
  91.                 break;
  92.             }
  93.         }
  94.     }
  95.  
  96.     return 0;
  97. }
  98.  
  99. ///ALHAMDULILLAH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement