Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Unsigned Long L...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 19 Topic 24535 of 27679
Post > Topic >>

Unsigned Long Long Overflow

by Tarique <peo_leo@[EMAIL PROTECTED] > Feb 8, 2008 at 09:04 PM

This program was compiled on MS Visual C++ 08

/*Fibonacci Numbers*/

#include<stdio.h>
#include<limits.h>

void fibonacci(int n)
{
	unsigned long long  fib0 = 0;	/*First Fibonacci Number*/
	unsigned long long fib1 = 1;	/*Second Fibonacci Number*/
	unsigned long long fibn = 1;	/*Nth Fibonacci Number*/
	int count =	3;	/*Hold Count*/
	
	printf("  1 :%25llu \n  2 :%25lld \n",fib0,fib1);

	while(count <= n )
	{
		fibn = fib0 + fib1 ;
		if((fibn < 0) || (fibn > ULLONG_MAX)){
			puts("\nOverflow\n");
			break;
		}
		printf("%3d :%25llu \n",count,fibn);
		fib0 = fib1;
		fib1 = fibn;
		count++;
	}
	return ;
}


int main(void)
{
	unsigned long temp = 0;
	
	puts("Fibonacci Numbers");
	fibonacci(100);	/*Print the first 100 Fibonacci Numbers*/
	
	return 0;
}

This is a part of the output :
Fibonacci Numbers

  ...snip...

  90 :      1779979416004714189
  91 :      2880067194370816120
  92 :      4660046610375530309
  93 :      7540113804746346429
  94 :     12200160415121876738
  95 :      1293530146158671551
  96 :     13493690561280548289
  97 :     14787220707439219840
  98 :      9834167195010216513
  99 :      6174643828739884737
100 :     16008811023750101250

Why are the numbers after 95th Fibonacci numbers (including it) wrong?
 




 19 Posts in Topic:
Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-08 21:04:58 
Re: Unsigned Long Long Overflow
Willem <willem@[EMAIL   2008-02-08 15:43:44 
Re: Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-08 21:30:52 
Re: Unsigned Long Long Overflow
santosh <santosh.k83@[  2008-02-08 21:16:19 
Re: Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-08 21:27:47 
Re: Unsigned Long Long Overflow
santosh <santosh.k83@[  2008-02-08 21:36:25 
Re: Unsigned Long Long Overflow
"Malcolm McLean"  2008-02-08 15:48:34 
Re: Unsigned Long Long Overflow
"christian.bau"  2008-02-10 11:04:16 
Re: Unsigned Long Long Overflow
santosh <santosh.k83@[  2008-02-08 21:21:31 
Re: Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-08 21:36:58 
Re: Unsigned Long Long Overflow
"Malcolm McLean"  2008-02-08 16:20:39 
Re: Unsigned Long Long Overflow
Willem <willem@[EMAIL   2008-02-08 16:26:08 
Re: Unsigned Long Long Overflow
santosh <santosh.k83@[  2008-02-08 22:20:32 
Re: Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-08 22:39:28 
Re: Unsigned Long Long Overflow
"Bartc" <bc@  2008-02-08 17:50:30 
Re: Unsigned Long Long Overflow
Richard Heathfield <rj  2008-02-08 18:19:34 
Re: Unsigned Long Long Overflow
Tarique <peo_leo@[EMAI  2008-02-09 01:11:36 
Re: Unsigned Long Long Overflow
"Bartc" <bc@  2008-02-08 21:00:53 
Re: Unsigned Long Long Overflow
Martin Ambuhl <mambuhl  2008-02-08 15:28:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 21:19:55 CDT 2008.