

#import <objc/Object.h>
#import "Fib.h"

@implementation Fib: Object 

+ new
{
   return [[[self alloc] init] useRecursive];
}

/* internal method */
- (int) recurse: (int) value
{
   int result = 1;

   if ( value  > 2 )
   {
      result = (int) [self recurse: ( value - 1 )]
             + (int) [self recurse: ( value - 2 )] ;
   }
   return result; 
}

/* internal method */
- (int) iterative: (int) value
{
   int i;
   int l1, l2, temp;
   l1 = 1;
   l2 = 1;

   for ( i = 3; i <= value; i++ )
   {
      temp = l1 + l2;
      l1 = l2;
      l2 = temp;
   }

   return l2;
}


- useRecursive
{
   method = 1;
   return self;
}

- useIterative
{
   method = 2;
   return self;
}

- (int) computeNew: (int) arg
{
   if ( method == 1 )
   {
      return [self recurse: arg];
   }
   else
   {
      return [self iterative: arg];
   }
}


@end
