Visit our sponsor Granny's Eggs  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


Program the Fib Sequence without recursion

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:

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
 

Add Your Comment

New Articles
  • Phone screen from MICROSOFT Denmark for SDET
    Technical Phone screen after Escreen

  • Please post some non-tech qs for User Experience and Business Analyst job

    I am a CS Grad applied in non-tech position.Please help!


  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • male enhancment buy viagra tramadol online
    Posted by SurojattSit on 2008年9月30日 (UMST)

  • penis enlargement viagra cheap tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)

  • penis viagra online Tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)



  •