Quarg

January 2nd, 2010, 02:21 AM

Hi

I've got this program Fibonacci.c that I'm using to mess with fibonacci numbers. I've got to deal with some huge numbers, and the eventual goal of the program is to find the Fibonacci number whose digital sum (adding each number, so 111 would be 1+1+1 = 3) meets or exceeds 100. So I'm basically writing a loop that finds fibonacci numbers, adds them in an array, sees what the digital sum is, and moving on if it doesn't get to 100. Here's the code:

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

#include<limits.h>

int FindFibonacciTerms(void)

{

bool done = false;

bool FibonacciDone = false;

int i;

int d;

unsigned long long int FibonacciNumberTwo;

unsigned long long int FibonacciSum;

unsigned long long int FibonacciNumber0 = 1;

unsigned long long int FibonacciNumber1 = 1;

unsigned long long int FibonacciNumber2 = 2;

unsigned long long int DigitalSum100 = 100000000000;

unsigned long long int FibonacciArray[12];

// while(FibonacciDone != true)

for(d = 0; d < 100; d++)

{

FibonacciNumber0 = FibonacciNumber1;

FibonacciNumber1 = FibonacciNumber2;

FibonacciNumber2 = FibonacciNumber0 + FibonacciNumber1;

printf("New FibonacciNumbers: %lld, %lld, %lld\n", FibonacciNumber2, FibonacciNumber1, FibonacciNumber0);

FibonacciNumberTwo = FibonacciNumber2;

for(i = 0; i < 12; i++)

{

FibonacciArray[i] = FibonacciNumberTwo % 10;

FibonacciNumberTwo = FibonacciNumberTwo / 10;

}

while(done = false)

{

for(i = 0; i < 12; i++);

{

if(FibonacciArray[i] > 9)

{

done = true;

}else {

FibonacciSum = FibonacciArray[i] + FibonacciSum;

}

}

}

if(FibonacciSum >= 100)

{

FibonacciDone = false;

} else {

FibonacciDone = true;

}

}

printf("Final FibonacciNumber = %lld\n", FibonacciNumber2);

return 0;

}

int main()

{

FindFibonacciTerms();

return 0;

}

my problem is, when it gets to really large numbers, it starts going crazy and giving me negative numbers that aren't Fibonacci numbers at all. I think I may be misusing my unsigned long long int's, but I don't know how. Any help?

I've got this program Fibonacci.c that I'm using to mess with fibonacci numbers. I've got to deal with some huge numbers, and the eventual goal of the program is to find the Fibonacci number whose digital sum (adding each number, so 111 would be 1+1+1 = 3) meets or exceeds 100. So I'm basically writing a loop that finds fibonacci numbers, adds them in an array, sees what the digital sum is, and moving on if it doesn't get to 100. Here's the code:

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

#include<limits.h>

int FindFibonacciTerms(void)

{

bool done = false;

bool FibonacciDone = false;

int i;

int d;

unsigned long long int FibonacciNumberTwo;

unsigned long long int FibonacciSum;

unsigned long long int FibonacciNumber0 = 1;

unsigned long long int FibonacciNumber1 = 1;

unsigned long long int FibonacciNumber2 = 2;

unsigned long long int DigitalSum100 = 100000000000;

unsigned long long int FibonacciArray[12];

// while(FibonacciDone != true)

for(d = 0; d < 100; d++)

{

FibonacciNumber0 = FibonacciNumber1;

FibonacciNumber1 = FibonacciNumber2;

FibonacciNumber2 = FibonacciNumber0 + FibonacciNumber1;

printf("New FibonacciNumbers: %lld, %lld, %lld\n", FibonacciNumber2, FibonacciNumber1, FibonacciNumber0);

FibonacciNumberTwo = FibonacciNumber2;

for(i = 0; i < 12; i++)

{

FibonacciArray[i] = FibonacciNumberTwo % 10;

FibonacciNumberTwo = FibonacciNumberTwo / 10;

}

while(done = false)

{

for(i = 0; i < 12; i++);

{

if(FibonacciArray[i] > 9)

{

done = true;

}else {

FibonacciSum = FibonacciArray[i] + FibonacciSum;

}

}

}

if(FibonacciSum >= 100)

{

FibonacciDone = false;

} else {

FibonacciDone = true;

}

}

printf("Final FibonacciNumber = %lld\n", FibonacciNumber2);

return 0;

}

int main()

{

FindFibonacciTerms();

return 0;

}

my problem is, when it gets to really large numbers, it starts going crazy and giving me negative numbers that aren't Fibonacci numbers at all. I think I may be misusing my unsigned long long int's, but I don't know how. Any help?