Example of O(n!)?

后端 未结 16 2445
渐次进展
渐次进展 2020-11-30 22:20

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I\'m asking a

16条回答
  •  青春惊慌失措
    2020-11-30 22:54

    @clocksmith You are absolutely correct. This is not calculating n!. Nor is it of O(n!). I ran it collected the data in the table below. Please compare column 2 and three. (#nF is the # of calls to nFacRuntimeFunc)

    n #nF n!

    0    0      1
    1    1      1
    2    4      2
    3    15     6
    4    65     24
    5    325    120
    6    1956   720
    7    13699  5040
    

    So clearly if performs much worse than O(n!). Below is the a sample code for calculating n! recursively. you will note that its of O(n) order.

    int Factorial(int n)
    {
       if (n == 1)
          return 1;
       else
          return n * Factorial(n-1);
    }
    

提交回复
热议问题