fork download
  1. using System;
  2.  
  3. namespace Symbolic
  4. {
  5. class Program
  6. {
  7. static char Char(int d)
  8. {
  9. return (char)(d + (int)'0');
  10. }
  11.  
  12. static int Digit(char c)
  13. {
  14. return (int)(c - '0');
  15. }
  16.  
  17. static string Multiply(string a, string b)
  18. {
  19. int[] acc = new int[a.Length + b.Length];
  20.  
  21. for (int i = a.Length - 1; i >= 0; i--)
  22. {
  23. int rest = 0;
  24. for (int j = b.Length - 1; j >= 0; j--)
  25. {
  26. int temp = acc[i + j + 1] + (Digit(a[i]) * Digit(b[j])) + rest;
  27. acc[i + j + 1] = temp % 10;
  28. rest = temp / 10;
  29. }
  30. acc[i] = acc[i] + rest;
  31. }
  32.  
  33. int start = 0;
  34. while (acc[start] == 0)
  35. {
  36. start++;
  37. }
  38.  
  39. char[] result = new char[acc.Length - start];
  40. for (int i = start; i < acc.Length; i++)
  41. {
  42. result[i - start] = Char(acc[i]);
  43. }
  44.  
  45. return new string(result);
  46. }
  47.  
  48. static string Previous(char c)
  49. {
  50. return Char(Digit(c) - 1).ToString();
  51. }
  52.  
  53. static string Decrement(string a)
  54. {
  55. string prev = Previous(a[0]);
  56.  
  57. if (a.Length == 1)
  58. {
  59. return prev;
  60. }
  61.  
  62. if (a[a.Length - 1] != '0')
  63. {
  64. var rest = a.Substring(0, a.Length - 1);
  65.  
  66. return rest + Previous(a[1]);
  67. }
  68. else
  69. {
  70. var rest = a.Substring(0, a.Length - 2);
  71.  
  72. if ((rest == "") && (prev == "0"))
  73. prev = "";
  74.  
  75. return rest + prev + '9';
  76. }
  77. }
  78.  
  79. static string Fact(string value)
  80. {
  81. string result = "1";
  82.  
  83. while (value != "0")
  84. {
  85. result = Multiply(result, value);
  86. value = Decrement(value);
  87. }
  88.  
  89. return result;
  90. }
  91.  
  92. static void Main(string[] args)
  93. {
  94. Console.WriteLine(Fact("30"));
  95. }
  96. }
  97. }
  98.  
Success #stdin #stdout 0.02s 14764KB
stdin
Standard input is empty
stdout
265252859812191058636308480000000