Search code examples
iosunsignedfactorial

iOS : unsigned long with big factorial


I'm trying to make factorial method like the following

-(void)getFactorial{
    NSString *numStr = [NSString stringWithFormat:@"%@", _maxTF.text];
    max = strtoull([numStr UTF8String], NULL, 0);
    for (unsigned long i = 1; i< max ; i++) {
        unsigned long factorial = [self factorial:i];
       if(factorial <= max){
          NSLog(@"%lu (%lu!  =  %lu)",i ,i ,factorial);
       }else{
          break;
       }
    }
}

-(unsigned long) factorial:(unsigned long)n
{
    if (n==0) {
        return 1;//TODO make it empty
    }
    else
    {
        return n*[self factorial:n-1];
    }
}

and it's simple and work fine.. till I try number like (18446744073709551615) which by all means should stop when i = 20, but the factorial of 21 is actually smaller than factorial of 20, so it go on till it reach zero.

I can't figure out where is the problem exactly .


Solution

  • The factorial is larger than the usigned long will allow, causing an overflow (starting at 0 again) making it appear to be smaller.