简体   繁体   中英

Arithmetic operation of value that exceeds limit in objective c

I have been trying the question on hacker rank in which addition of all the substring of a string is taken into account. In doing so our answer exceeds the limit of NSInteger, NSUInteger, long, very very long, double.

So in this case i tried

- (NSDecimalNumber *) substrings:(NSString *)n {

    NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    if ([n rangeOfCharacterFromSet:notDigits].location == NSNotFound) {

        NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
        f.numberStyle = NSNumberFormatterNoStyle;
        NSDecimalNumber *sum = [NSDecimalNumber decimalNumberWithString:@"0"];
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i<n.length; i++) {

            for (NSInteger j = 1; j<=n.length-i; j++) {

                NSString *stringInRange = [n substringWithRange:NSMakeRange(i, j)];
                [arr addObject:stringInRange];
                NSDecimalNumber *myNumber1 = [NSDecimalNumber decimalNumberWithString:stringInRange];
                sum = [sum decimalNumberByAdding:myNumber1];
            }
        }
        return sum;
    } else {

        return nil;
    }
}

But still, I am getting the wrong answer of addition. I want to know in such cases where test cases include much heavier value than the limits, what API should we use to perform arithmetic operation?

What I have tried is a correct approach or not?

If you need to operate with big integer values in Objective-C you can use JKBigInteger . For example, you can multiply 2 big integers and convert result to NSString with JKBigInteger. Here you can see how to use this library:

JKBigInteger *bigInteger1 = [[JKBigInteger alloc] initWithString:@"2"];
JKBigInteger *bigInteger2 = [[JKBigInteger alloc] initWithString:@"3"];
JKBigInteger* bigInteger3 = [bigInteger1 add:bigInteger2];
NSLog(@"%@", [bigInteger3 stringValue]); // output is 5

And I guess you can get values that you need with this function:

-(JKBigInteger*)sumOfSubValues:(JKBigInteger*)value {
    JKBigInteger* currentValue = [[JKBigInteger alloc] initWithString:value.stringValue];
    JKBigInteger* result = [[JKBigInteger alloc] initWithString:currentValue.stringValue];
    NSUInteger digitsCount = currentValue.stringValue.length - 1;
    JKBigInteger* const tenValue = [[JKBigInteger alloc] initWithString:@"10"];
    while (digitsCount != 0) {
        JKBigInteger* const firstDigit = [[JKBigInteger alloc] initWithString:[currentValue.stringValue substringToIndex:1]];
        JKBigInteger* const firstDigitPowerBase = [tenValue pow:(unsigned int)digitsCount];
        JKBigInteger* const firstDigitPower = [firstDigit multiply:firstDigitPowerBase];
        JKBigInteger* const diff = [currentValue subtract:firstDigitPower];
        currentValue = diff;
        digitsCount--;
        result = [result add:currentValue];
    }
    return result;
}

You can test it with this code snippet:

JKBigInteger* result = [self sumOfSubValues:[[JKBigInteger alloc] initWithString:@"1234"]];
NSLog(@"%@", result.stringValue); // output is "1506" (1234 + 234 + 34 + 4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM