fork download
  1. using System;
  2.  
  3. namespace Factorial
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Factorial<intFact> f = new Factorial<intFact>(new intFact(5));
  10. Console.WriteLine(f.Value);
  11. }
  12.  
  13. }
  14.  
  15. struct intFact : IFactoriable<intFact>
  16. {
  17. public int n;
  18. public intFact(int num)
  19. {
  20. n = num;
  21. }
  22. public bool isZero()
  23. {
  24. return n == 0;
  25. }
  26. public bool isBelowZero()
  27. {
  28. return n < 0;
  29. }
  30. public intFact Step()
  31. {
  32. return new intFact(n - 1);
  33. }
  34. public intFact Multiply(intFact iF)
  35. {
  36. return new intFact(n * iF.n);
  37. }
  38. public intFact getNeutralElement()
  39. {
  40. return new intFact(1);
  41. }
  42. public override string ToString()
  43. {
  44. return n.ToString();
  45. }
  46. }
  47.  
  48. [Serializable]
  49. class Factorial<T> where T : IFactoriable<T>
  50. {
  51. T p;
  52.  
  53. public T Value
  54. {
  55. get;
  56. private set;
  57. }
  58. public T Param
  59. {
  60. get
  61. {
  62. return p;
  63. }
  64. set
  65. {
  66. if (value.isBelowZero()) throw new ArgumentOutOfRangeException("n or Param");
  67. Value = fact(value);
  68. p = value;
  69. }
  70. }
  71.  
  72. public Factorial(T n)
  73. {
  74. Param = n;
  75. }
  76.  
  77. T fact(T n)
  78. {
  79. if (n.isZero()) return n.getNeutralElement();
  80. return n.Multiply(fact(n.Step()));
  81. }
  82. }
  83.  
  84. interface IFactoriable<T>
  85. {
  86. bool isZero();
  87. bool isBelowZero();
  88. T Step();
  89. T getNeutralElement();
  90. T Multiply(T t);
  91. }
  92. }
Success #stdin #stdout 0.03s 37032KB
stdin
Standard input is empty
stdout
120