Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- @interface Fibonacci: NSObject {
- NSMutableArray *calculated;
- }
- - (int) getNumber: (int) n;
- @end
- @implementation Fibonacci
- - (id) init {
- self = [super init];
- calculated = [NSMutableArray array];
- for (int i = 0; i < 2; i++) {
- [calculated addObject: [NSNumber numberWithInt: 1]];
- }
- return self;
- }
- - (int) getNumber: (int) n {
- if (n > calculated.count - 1) {
- [calculated addObject: [NSNumber numberWithLong: [self getNumber: n - 1] + [self getNumber: n - 2]]];
- }
- return [[calculated objectAtIndex: n] intValue];
- }
- @end
- int main() {
- Fibonacci *fib = [[Fibonacci alloc] init];
- for (int i = 0; i < 25; i++) {
- NSLog(@"\nThe %dth number in the Fibonacci Sequence is %d", i, [fib getNumber: i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment