0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811…
Program it for efficiency and precision. Also nice and eloquent solutions are good too.
Comments: Flat Nested Threaded Embedded Oldest First Newest First code By kanakshila on Sunday, June 24, 2007 (UMST) Fibonacci Series #include<iostream.h>#include<conio.h> main(){ const unsigned long limit=4294967295; unsigned long next=0; unsigned long last=1; long sum; clrscr(); cout<<"\n\nThis program will print the Fibonacci series :\n\n "; while(next<limit/2) { cout<<last <<" "; sum=next+last; next=last; last=sum; } Reply to this Comment Average Rating: why not using DP?? By kallol on Sunday, July 01, 2007 (UMST) #include<stdio.h> #define LIMIT 100 unsigned long fib[LIMIT]; int main(void) { fib[0]=0; fib[1]=1; int i; for(i=2;i<LIMIT;i++) { fib[i]=fib[i-1]+fib[i-2]; } for(i=0;i<LIMIT;i++) { printf("%lu ",fib[i]); } return 0; } Reply to this Comment Is it right ? By sforsawant on Saturday, August 25, 2007 (UMST) #include<stdio.h> void main() { unsigned long a=0,b=1; while(a<=4294967295 && b<=4294967295) { printf("%ld \t %d \t", a,b); a=a+b; b=a+b; } } Reply to this Comment Average Rating: elegant solution By jagatsastry on Thursday, August 30, 2007 (UMST) void fib(int n) { long int first=0, second=1; for(int i=0;i<n;i++) { printf("%ld",second); long int temp=second; second=second+first; first=temp; } } Reply to this Comment C# By Leon on Monday, September 24, 2007 (UMST) static void Main(string[] args) { int n=0; int i = 1; int j = 0; System.Console.WriteLine(0); System.Console.WriteLine(i); while(n<=10) //need to print how may number { if (j < i) j = j + i; else i = j + i; if (i >= j) System.Console.WriteLine(i); else System.Console.WriteLine(j); n++; } } Reply to this Comment Using overflow :-) By christophilus on Sunday, October 28, 2007 (UMST) #include <iostream>;using namespace std; void main(){ unsigned long x = 0, y = 1; while (y > x) { cout<< x << ", " << y << ", "; x += y; y += x; } cout<< x;} Reply to this Comment fibonacii series with recursion function By budhvani-mahendra on Saturday, November 17, 2007 (UMST) #include<iostream.h> int main() { int a=0,b=1,c=0; int no; cout<<"Enter the No of Terms :>"; cin>>no; while(no) { a=b; b=c; cout<<" "<<b; c=a+b; } return 0; } Reply to this Comment With and without recursion By akash on Friday, January 04, 2008 (UMST) //============================================================================ // Name : fibonacci.cpp // Author : Akash Tiwari // Version : http://www.cise.ufl.edu/~atiwari // Copyright : (c) Akash Tiwari // Description : Fibonacci with and without recursion in C++, Ansi-style //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584 //============================================================================ #include <iostream> #include <memory> using namespace std; class fibonacci{ int limit; int count; public: fibonacci(): limit(0), count(0) {}; fibonacci(int l) : limit(l){}; void fibonacci_recursion(int, int); void fibonacci_without_recursion(); }; void fibonacci::fibonacci_recursion(int a, int b){ if(limit==1){ cout<<a<<" "; return; } if(limit==2){ cout<<a<<" "<<b<<" "; return; } cout<<a<<" "; count++; //cout<<a+b<<" "; if(count==limit) return; fibonacci_recursion(b,(a+b)); } void fibonacci::fibonacci_without_recursion(){ int a=0,b=1,c=0,count=0; cout<<"0 "; if(limit==1){ return; } cout<<"1 "; if(limit==2){ return; } count=2; while(count!=limit){ c=a+b; cout<<c<<" "; a=b;b=c;count++; } } int main() { int n; cout << "Enter the limit for fibonacci limit: "; cin>>n; auto_ptr<fibonacci> p (new fibonacci(n)); p->fibonacci_without_recursion(); cout<<"\n Recursive call \n"; p->fibonacci_recursion(0,1); return 0; } Reply to this Comment without recursion By chauhan on Sunday, January 20, 2008 (UMST) int x=0,y=1; int i=0; int temp; cout<<"1"; while(i<10) { temp=y; y=x+y; x=y-x; cout<<" "<<y; i++; } Reply to this Comment ignore temp By chauhan on Sunday, January 20, 2008 (UMST) int x=0,y=1; cout<<" "<<"1"; for(int i=0;i<10;i++) { y=x+y; x=y-x; cout<<" "<<y; } Reply to this Comment Average Rating: without Recurssion (C#) By smartyuppy on Wednesday, March 26, 2008 (UMST) Check boundary conditions using uint.GetMaxValue() public static uint fib(uint i) { uint iminus1= 1; uint iminus2 = 1; if (i < 2) return i; uint sum = 1; for(uint m=0;m<i-2; m++) { steps++; sum = iminus1 + iminus2; iminus2 = iminus1; iminus1 = sum; } return sum; } Reply to this Comment O(log n) solution By ksumit on Saturday, June 14, 2008 (UMST) There is another way to generate upto nth fibonacci number in O(log n) time. It uses Fibonacci Q-matrix as mentioned in http://mathworld.wolfram.com/FibonacciQ-Matrix.html (also in Coremen) Reply to this Comment NonRecursiveFibon By PrinceOfWildCats on Wednesday, October 01, 2008 (UMST) int NonRecursiveFibon(int n) { if(n == 0) return 0; if(n == 1) return 1; int Sum = 1; int nFirst = 0; int nSecond = 1; for(int k = 0; k <n; k++) { Sum = nFirst+nSecond; nSecond = nFirst; nFirst = Sum; } return Sum; } Reply to this Comment
Fibonacci Series
#include<iostream.h>#include<conio.h>
main(){ const unsigned long limit=4294967295; unsigned long next=0; unsigned long last=1; long sum;
clrscr();
cout<<"\n\nThis program will print the Fibonacci series :\n\n "; while(next<limit/2) { cout<<last <<" "; sum=next+last; next=last; last=sum; }
Reply to this Comment
#include <iostream>;using namespace std;
void main(){ unsigned long x = 0, y = 1;
while (y > x) { cout<< x << ", " << y << ", "; x += y; y += x; }
cout<< x;}
#include<iostream.h>
int main()
{
int a=0,b=1,c=0;
int no;
cout<<"Enter the No of Terms :>";
cin>>no;
while(no)
a=b;
b=c;
cout<<" "<<b;
c=a+b;
}
return 0;
int
Sum = nFirst+nSecond;
nSecond = nFirst;
nFirst = Sum;