Skip to content

Instantly share code, notes, and snippets.

@Prince781
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save Prince781/5c5fb12b4cfad31fde7c to your computer and use it in GitHub Desktop.

Select an option

Save Prince781/5c5fb12b4cfad31fde7c to your computer and use it in GitHub Desktop.
Fast factorial implementation (using 1/2 iterations) in C
typedef unsigned long uli;
uli fact(uli n) {
if (n < 2) return 1;
uli r = (n % 2) ? 1+n/2 : 1; // if odd...
uli p = n, a = n, b = n-2;
while (b>1) {
p *= a + b;
a += b;
b -= 2;
}
return p * r; // ...multiply by mid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment