Saturday 11 October 2014

The first fibonacci series term that can't be accommodated in Integer's Range

The problem is to find the first Fibonacci series term that can't be accommodated in the range of Integers i.e. for a 32-bit compiler , the number in Fibonacci series must exceed 2147483647.

Solution:
 
The concept that has been applied here is that at any point, if the sum of the two consecutive terms of Fibonacci series becomes less than the second term, then we have found the term. 

Source Code:
*******************************************

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

int main()
{
    int counter=2;
    int first=0,second=1;
    int sum;
    while(1)
    {
        sum=first+second;
        printf("%d  %d\n",(counter+1),sum);
        if(sum>=second)
        {
            counter++;
        }
        else
            break;
        first=second;
        second=sum;
    }
    counter++;
    printf("\nThe first term that can't be accommodated in integers range is:%d",counter);
    return 0;
}
 

*******************************************

The answer is 48. The 48th Fibonacci term is not in the range of integers for a 32-bit compiler.

No comments:

Post a Comment