Skip to content

Instantly share code, notes, and snippets.

@madson
Created October 21, 2011 17:21
Show Gist options
  • Save madson/1304386 to your computer and use it in GitHub Desktop.
Save madson/1304386 to your computer and use it in GitHub Desktop.
Factorial calc with objective-c
#include <Foundation/Foundation.h>
@interface Math : NSObject
+ (int)factorial:(int)n;
@end;
@implementation Math
+ (int)factorial:(int)n
{
if (n == 0)
return 1;
return n * [Math factorial:n-1];
}
@end
int main() {
int number = 5;
int result = [Math factorial:number];
printf("The factorial number of %i is %i!", number, result);
return 0;
}
@AlbertRenshaw
Copy link

AlbertRenshaw commented Jul 30, 2018

-(float)factorial:(float)number {
    return tgammaf(++number);
}

//Works for floats and negatives too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment