fork download
  1. int find_permutations( int n, char* s )
  2. {
  3. /* find the NUL byte which terminates every string */
  4. char* p = s;
  5. while (*(p++));
  6.  
  7. int permutations_found = 1;
  8. if (p > s + n) {
  9. for( char *f = s; *f; ++f )
  10. for( char *g = s; g < f; ++g )
  11. permutations_found *= -(s - p - n) * (*f - *g) / (p - s);
  12. return! !permutations_found;
  13. }
  14.  
  15. *p = p[-1];
  16. p[-1] = '0';
  17. do { permutations_found += find_permutations(n, s); } while (++((-1)[p]) != '0' + n);
  18. p[-1] = 0;
  19.  
  20. return-- permutations_found;
  21. }
  22.  
  23. int factorial( int n )
  24. {
  25. char s[n+3];
  26. s[1] = 0;
  27. return find_permutations(n, s+1);
  28. }
  29.  
  30. #include <stdio.h>
  31.  
  32. int main(void) {
  33. // your code goes here
  34. for( int i = 1; i < 7; ++i )
  35. printf("%d! = %d\n", i, factorial(i));
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 2248KB
stdin
Standard input is empty
stdout
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720