Common factorial function in C #

I want to write a generalized function for calculating factorial in C # ... for example:

static T Factorial<T>(T n) { if (n <= 1) return 1; return Factorial<T>(n - 1); } 

but obviously has a limitation on the fact that we cannot perform operations on type ' T '. any alternative?

+4
source share
7 answers
  public T Factorial<T>(T a, T b, Multiply<T> delegateMutliply, Difference<T> diffDelegate, BaseCondition<T> baseDelegate) { if (!baseDelegate(a, b)) return b; return delegateMutliply(a, Factorial<T>(diffDelegate(a), b, delegateMutliply, diffDelegate, baseDelegate)); } 

int y = p.Factorial (3, 1, (a, b) => a * b, (a) => - a, (a, b) => (a <= b)? false: true);

-1
source

The problem is that generics do not support statements because they are static methods and not part of the interface. However, you could use the General Operators , which is available in a miscellaneous useful library .

+7
source

You will need to add a delegate parameter that does the multiplication. Something like that

 delegate T Multiply<T>(T a, T b); 

So, your function will be defined as follows:

 static T Factorial<T>(T n, Multiply func) { ... your code here } 

So, when you call the factorial function, the caller goes into the multiplication function:

 int x = Factorial<int>(5, (a,b) => a * b); 
+5
source

There is no easy way to do this. I have seen some solutions that work around the problem, but they are quite complex. However, if you really want to do this, here are a few ideas:

  • If you can use .Net 4, you can apply n to dynamic , and then do the addition. Of course you lose security - you may get an exception at runtime

  • You can always manually check the type from your factorial function: if n is short, discarded to short, if n is double, applies to double ... etc. It is complex and loses part of the value of generics, but the external API at least looks simple.

+2
source

When was the last time you took a string factorial or character? Why do you need a factorial like T ??

In addition, it has been said numerous (now 1 million times). When you need to use a generic type, you need to tell the compiler the type.

For example, if I had a generic stack class? C # needs to know the type of elements when creating my stack.

Otherwise, it does not make sense for:

 Stack<T> s = new Stack<T>(); s.Push(????); //how do I add items to the stack if we don't know what T is? 

Instead, you need to specify:

 Stack<int> s = new Stack<int>(); s.Push(5); s.Push(7); 
+1
source

This does not apply specifically to your question on how to create a general method, but your method in its current form will never return anything but 1.

Suppose you only work with integers, it should look like this:

 static int Factorial(int n) { if (n <= 1) return 1; // note the multiplication in this step return n * Factorial(n - 1); } 
+1
source

Super old question, but I would like to add my 2 cents.

Factor functions require f* = i

A quick function for the above example:

  class FindFactorial { static void Main() { Console.WriteLine("Please enter your number: "); int n = int.Parse(Console.ReadLine()); int factorial = 1; for (int i = 1; i <= n; i++) { factorial *= i; } Console.WriteLine(factorial); } } 
0
source

Source: https://habr.com/ru/post/1303568/


All Articles