Last active
August 29, 2015 14:02
-
-
Save Prince781/5c5fb12b4cfad31fde7c to your computer and use it in GitHub Desktop.
Fast factorial implementation (using 1/2 iterations) in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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