Example of O(n!)?
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); } }
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); } }
This will work for the factorial (although a very small subset) of positive integers: unsigned long factorial(unsigned long f) { if ( f == 0 ) return 1; return(f * factorial(f – 1)); } printf(“%i”, factorial(5)); Due to the nature of your problem (and level that you have admitted), this solution is based more in … Read more
Factorials get large real fast: >>> math.factorial(170) 7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000L Note the L; the factorial of 170 is still convertable to a float: >>> float(math.factorial(170)) 7.257415615307999e+306 but the next factorial is too large: >>> float(math.factorial(171)) Traceback (most recent call last): File “<stdin>”, line 1, in <module> OverflowError: long int too large to convert to float You could … Read more
n can be arbitrarily large Well, n can’t be arbitrarily large – if n >= m, then n! ≡ 0 (mod m) (because m is one of the factors, by the definition of factorial). Assuming n << m and you need an exact value, your algorithm can’t get any faster, to my knowledge. However, if … Read more
You can search for (1…100)! on Wolfram|Alpha to pre-calculate the factorial sequence. The first 100 numbers are: 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000, 51090942171709440000, 1124000727777607680000, 25852016738884976640000, 620448401733239439360000, 15511210043330985984000000, 403291461126605635584000000, 10888869450418352160768000000, 304888344611713860501504000000, 8841761993739701954543616000000, 265252859812191058636308480000000, 8222838654177922817725562880000000, 263130836933693530167218012160000000, 8683317618811886495518194401280000000, 295232799039604140847618609643520000000, 10333147966386144929666651337523200000000, 371993326789901217467999448150835200000000, 13763753091226345046315979581580902400000000, 523022617466601111760007224100074291200000000, … Read more
GNU Multiprecision library is a good one! But since you say using of external libraries are not allowed, only way I believe its possible is by taking an array of int and then multiplying numbers as you do with pen on paper! Here is the code I wrote some time back.. #include<iostream> #include<cstring> int max … Read more
I have a solution: (4N!)=((2N!)^2) . mul(i=all primes<=4N) of [i^sum(j=1,2,3,4,5,…4N>=i^j) of [(4N/(i^j))%2]] sub-terms of T2 are always prime^exponent where exponent can be computed on small integers like this: for (e=0,j=N4;j;e+=j&1,j/=p); where e is exponent, p is prime and N4 is 4*N Code for the new equation: // edit beg: // Sorry, forget to copy sorted … Read more