C language-function that returns unsigned type for a big Fibonacci number (like the 89 element)

170 Views Asked by At

I am new to C but doing some exercises (online and run to see if test passed) so this exercise is as follow

Requirements:

  • write a function with return type unsigned and parameter unsigned (must use this type) -need to be able to get (return) the nth Fibonacci number for example 89 element (if first 3 elements are 0, 1, 1 the 89 element is 1,100,087,778,366,101,931).

Problem:

  • saw some answers of using "unsigned long long" but the requirements don't allow using that type. Also can't use external libraries lime GMP

Need help what can I do?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned Fibonacci(unsigned numElement);

int main()
{
    unsigned element;
    element = Fibonacci(89);
    printf("%d\n",element);

    return 0;
}

unsigned Fibonacci(unsigned numElement){

    int fibonacciArr[100] = {0, 1};
    int i;

    for(i = 2; i < numElement; i++ ){
                fibonacciArr[i] = fibonacciArr[i-1] + fibonacciArr[i-2];
    }

    return fibonacciArr[numElement -1];
}

Get output:-2092787285 (i understand is out of range for unsigned int so is negative but not sure what to do to fix this) Instead of expected out:1100087778366101931

-tried to use "unsigned long long" but don't fit exercise requirement -change from first element to 1 (instead of 0 and worked) but thank you all

2

There are 2 best solutions below

3
julaine On

Those requirements as you understand them are simply impossible, because, as you say, the correct result is too big for a 32-bit-unsigned-integer.

There are several ways to interpret the task you were given:

  1. Unsigned does not literally mean 'unsigned int', it just means 'an unsigned integer type' and therefore 'unsigned long long' would be allowed.
  2. Use 'unsigned', then claim your answer only works on C-implementations where 'unsigned' is 64bits as that is allowed by the standard. Insist that your answer is a correct C-program, even though none of the standard compilers will compile it correctly.
  3. The 89 was an example of fibonacci numbers in general, not an example input to your function, and failing on 89 is acceptable.
  4. An overflowed result is considered a correct result. In that case, you need to use unsigned everywhere, including your int-array. Overflowing an unsigned type is valid C, Overflowing a signed type is undefined behaviour.
0
chux - Reinstate Monica On

With 3 changes, code is able to get the intended output of "1100087778366101931" using unsigned Fibonacci(unsigned numElement);.

Perhaps not likely what was intended yet, somehow we need a 64-bit answer.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Add
#define unsigned unsigned long long

unsigned Fibonacci(unsigned numElement);

int main() {
  unsigned element;
  element = Fibonacci(89);
  // printf("%d\n",element);
  printf("%llu\n", element);
  return 0;
}

unsigned Fibonacci(unsigned numElement) {
  // int fibonacciArr[100] = {0, 1};
  unsigned fibonacciArr[100] = {0, 1};
  int i;

  for (i = 2; i < numElement; i++) {
    fibonacciArr[i] = fibonacciArr[i - 1] + fibonacciArr[i - 2];
  }
  return fibonacciArr[numElement - 1];
}