Skip to content

Instantly share code, notes, and snippets.

@itolosa
Created February 14, 2016 01:34
Show Gist options
  • Save itolosa/f274efab5415569fc4f7 to your computer and use it in GitHub Desktop.
Save itolosa/f274efab5415569fc4f7 to your computer and use it in GitHub Desktop.
My first recursion in Objective-C
//
// main.m
//
// Created by Ignacio Tolosa on 2/13/16.
// Copyright © 2016 PasajesLA. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MyMathClass : NSObject
-(int) factorial: (int) n;
@end
@implementation MyMathClass
-(int) factorial: (int) n
{
return n > 1 ? [self factorial: n-1] * n : 1;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyMathClass *m = [[MyMathClass alloc] init];
for (int i = 0; i < 10; i++) {
NSLog(@"Factorial de %d es %d\n", i, [m factorial: i]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment