因此,我一直在研究 Objective-C 中的 Project Euler 问题 25,但遇到了NSDecimalNumber的大小限制。因此,在尝试使用其他代码库后,我决定重新表示我的斐波那契数,首先是 s 的数组,int
然后NSArray
是 s 的NSNumber
s。数组中的每个单元格都是一个数字。然后我可以将大数逐位相加,直到第 1000 位。不幸的是出了点问题,我的进位是正确的,但最后一个进位之前的数字似乎总是为零。
我试过计算第 8 个斐波那契数和第 12 个斐波那契数并得到相同的行为。我没有得到 13 和 144,而是得到 10 和 104。我正确地对数字求和,但是
[nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex];
似乎没有做我期望的事情。digitSum 在这两种情况下都是 3 和 4,但是一旦我的方法将下一个斐波那契数作为 NSArray 返回,我就会得到一个 0,而我预计是 3 或 4。我已经尝试过它,但我很困惑。它似乎在某些时候有效,但其他时候我动态创建的 NSNumber 没有我期望的值。这是我的整个方法:
+ (NSArray*) nextBigFibonancci: (NSArray*) fibZero After: (NSArray*) fibOne
{
// It's come to manually adding digits in one thousand count arrays.
// I can't return or pass arrays... will have to use NSArrays for everything, version at least 4.0
// Since XCode 4.5 I can use array[i] and other array literals... Lets just get it working...
// Works for Fib 1 and Fib 2 and Fib 3, but not Fib 12...
int fibZeroDigits = [fibZero count];
int fibOneDigits = [fibOne count];
NSMutableArray* nextFib = [NSMutableArray arrayWithCapacity:1000];
NSArray* number;
int arrayIndex = 999;
int cellCount = fibZeroDigits - 1;
int digitZero, digitOne, digitSum;
// There is an Objective-c loop structure for looping through all objects in array, but stick to this...
for (int j = 0; j < 1000; j++)
{
// All the integers must be zero.
[nextFib insertObject: [NSNumber numberWithInt:0] atIndex: j];
}
for (int i = fibOneDigits; i > 0; i--)
{
digitZero = [[fibZero objectAtIndex: cellCount] intValue];
digitOne = [[fibOne objectAtIndex: i - 1] intValue];
NSLog(@"arrayIndex is: %i", arrayIndex); // arrayIndex seems correct why am I getting 104?
if (digitZero + digitOne < 10)
{
digitSum = digitZero + digitOne + [[nextFib objectAtIndex: arrayIndex] intValue];
[nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex];
}
else
{
digitSum = digitZero + digitOne - 10 + [[nextFib objectAtIndex:arrayIndex] intValue];
// This isn't working the second time, though digitSum is added correctly...
// Getting 1,0,4 for fibTwelve instead of 144
// Doesn't work for fibEight get 1,0 instead of 13...
[nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex];
[nextFib insertObject: [NSNumber numberWithInt: 1] atIndex: arrayIndex -1];
}
arrayIndex = arrayIndex - 1;
cellCount = cellCount - 1;
}
// Must carry the last digit in fibOne if arrays are of different sizes...
if (fibZeroDigits < fibOneDigits)
{
// fibOne has one extra digit
digitSum = [[fibOne objectAtIndex:0] intValue] + [[nextFib objectAtIndex:arrayIndex - 1] intValue];
[nextFib insertObject:[NSNumber numberWithInt:digitSum] atIndex:arrayIndex -1];
}
// Shouldn't return nextFib, but only the signifigant, ie non zero integers
// Find first non zero digit and then the range from there until the end of the array nextFib
for(int n = 0; n < 1000; n++)
{
if ([[nextFib objectAtIndex: n] intValue] > 0)
{
// First non zero digit.
NSRange theRange;
theRange.location = n;
theRange.length = 1000 - n;
number = [nextFib subarrayWithRange:theRange];
break; // Could set n = 1000 which would also break...
}
}
return number;
}
任何想法为什么digitSum
和NSNumber
我用它创建的东西在需要携带时没有按预期存储?