Example of O(n!)?

Derek Long picture Derek Long · Oct 17, 2010 · Viewed 57.3k times · Source

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 about time complexity.

Answer

sepp2k picture sepp2k · Oct 17, 2010

There you go. This is probably the most trivial example of a function that runs in O(n!) time (where n is the argument to the function):

void nFacRuntimeFunc(int n) {
  for(int i=0; i<n; i++) {
    nFacRuntimeFunc(n-1);
  }
}