Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:13:22+00:00 2026-06-18T11:13:22+00:00

So I’ve been working on Project Euler problem 25 in Objective-C and I ran

  • 0

So I’ve been working on Project Euler problem 25 in Objective-C and I ran up against the size limit of NSDecimalNumber. So after attempting to use other code libraries I decided to re-represent my Fibonacci number, first as arrays of ints then NSArrays of NSNumbers. Each cell in the array would be one digit. Then I could add to large numbers together digit by digit up to the 1000th digit. Unfortunately something goes wrong, my carry is correct but the digit before the last carry seems to always be zero.

I’ve tried calculating the 8th Fibonacci number and the 12th Fibonacci number and get the same behaviour. Instead of getting 13 and 144 I get 10 and 104. I sum the digits correctly but

[nextFib insertObject: [NSNumber numberWithInt:digitSum] atIndex: arrayIndex]; 

Doesn’t seem to be doing what I expect. digitSum is a 3 and a 4 in both instances, but once my method returns the next fibonacci number as an NSArray I have a 0 where I expected a 3 or a 4. I’ve tried stepping through it, and I’m baffled. It seems to work some of the time but other times the NSNumber I create on the fly is not having the value I expect. Here is my entire method:

+ (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;
}

Any ideas why digitSum and the NSNumber I create with it isn’t getting stored as expected when carrying is necessary?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:13:24+00:00Added an answer on June 18, 2026 at 11:13 am

    As alluded to above, I found my own bug and several more. insertObjectAtIndex adds an entirely new object to the NSMutableArray, I instead wanted to replaceObjectAtIndexWith which replaced the previous digit with the newly calculated digit.

    XCode just updated adding the ability to look inside NSArrays from the debugger which would have been nice as I also mentioned above. Here is the method for adding two 1000 digit numbers, it could be modified to add even larger numbers, they don’t have to even be Fibonacci numbers.

    + (NSArray*) nextBigFibonancci: (NSArray*) fibZero After: (NSArray*) fibOne
    {    
        int fibZeroDigits = [fibZero count];
        int fibOneDigits = [fibOne count];
        int loops = fibZeroDigits - 1;
        int fibOneIndex = loops;
        NSMutableArray*  nextFib = [NSMutableArray arrayWithCapacity:1000];
        NSArray* number;
        int arrayIndex = 999;
        int digitZero, digitOne, digitSum;
    
        // There is an Objective-c loop structure for looping through all objects in array, but I'll just stick to this...
        for (int j = 0; j <= arrayIndex; j++) 
        {
            // All the integers start at zero.
            [nextFib insertObject: [NSNumber numberWithInt:0] atIndex: j];
        }
    
        if (fibOneDigits > fibZeroDigits)
        {
            fibOneIndex++;
        }
    
        for (int i = loops; i >= 0; i--)
        {
            digitZero = [[fibZero objectAtIndex: i ] intValue];
            digitOne = [[fibOne objectAtIndex: fibOneIndex ] intValue];
            // Have to use replaceObjectAtIndex not insertObjectAtIndex!
            digitSum = digitZero + digitOne + [[nextFib objectAtIndex: arrayIndex] intValue];
            if (digitSum < 10)
            {
                [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]];
            }
            else
            {
                digitSum = digitSum - 10;
                [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]];
                [nextFib replaceObjectAtIndex: arrayIndex -1 withObject: [NSNumber numberWithInt:1]];
            }
            arrayIndex = arrayIndex - 1;
            fibOneIndex = fibOneIndex -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] intValue];
            [nextFib replaceObjectAtIndex: arrayIndex withObject: [NSNumber numberWithInt:digitSum]];
        }
    
        // 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.