Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Fibonacci with no recursion for fun

  1. #1
    HAL 9000 is offline Newbie
    Join Date
    Jun 2008
    Posts
    17
    Rep Power
    0

    Smile Fibonacci with no recursion for fun

    I made a non recursive Fibonacci function in C for fun because it can't solve 100 fast with recursion.
    Did anyone else besides me do it this way
    Code:
    #include<stdio.h>
    
    /*prototypes*/
    double fib(int n);
    
    int main(void)
    {
    	int i;
    	
    	for(i = 0;i <= 100;++ i)
    	{
    		printf("%1.0f \n",fib(i));
    	}
    
    	return 0;
    }
    
    /*non recursive fibonacci function*/
    double fib(int n)
    {
    	double prev = -1;
    	double result = 1;
    	double sum;
    	int i;
    	
    	for(i = 0;i <= n;++ i)
    	{
    		sum = result + prev;
    		prev = result;
    		result = sum;
    	}
    	
    	return result;
    }
    Last edited by HAL 9000; 06-23-2008 at 01:32 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    samy is offline Newbie
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Re: Fibonacci with no recursion for fun

    Hi HAL 9000!

    Your code looks good. I have not tried your code but it seems to be working. I want to ask a question (just for sharing fun along with you) as to how long your code can extent i.e until what number.

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fibonacci with no recursion for fun

    I tend to do factorials, permutations, and combinations in loops for the same reason.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    samy is offline Newbie
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Re: Fibonacci with no recursion for fun

    Hi WingedPanther!

    Would you like to share the fun with us by giving the program for factorials, permutations, and combinations?

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Fibonacci with no recursion for fun

    Quote Originally Posted by HAL 9000 View Post
    I made a non recursive Fibonacci function in C for fun because it can't solve 100 fast with recursion.
    Did anyone else besides me do it this way
    Code:
    #include<stdio.h>
    
    /*prototypes*/
    double fib(int n);
    
    int main(void)
    {
    	int i;
    	
    	for(i = 0;i <= 100;++ i)
    	{
    		printf("%1.0f \n",fib(i));
    	}
    
    	return 0;
    }
    
    /*non recursive fibonacci function*/
    double fib(int n)
    {
    	double prev = -1;
    	double result = 1;
    	double sum;
    	int i;
    	
    	for(i = 0;i <= n;++ i)
    	{
    		sum = result + prev;
    		prev = result;
    		result = sum;
    	}
    	
    	return result;
    }
    A few months ago, I posted this:

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    //Non-recrusive: O(1)
    unsigned long fib(int n)
    {
         return (1/sqrt(5)) * (pow(((1 + sqrt(5)) / 2), n) - pow(((1 - sqrt(5)) / 2), n));
    }
    
    //Recrusive: O(n)
    unsigned long fib2(int n)
    {
        if(n <= 2) return 1;
        else return fib2(n - 1) + fib2(n - 2);
    }
    
    //Iterative: O(n)
    unsigned long fib3(int n)
    {
        int u = 0;
        int v = 1;
        int i, t;
    
        for(i = 2; i <= n; i++)
        {
            t = u + v;
            u = v;
            v = t;
        }
        return v;        
    }
    Last edited by dargueta; 11-26-2010 at 02:12 PM. Reason: Fixed code tags

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fibonacci with no recursion for fun

    Quote Originally Posted by samy View Post
    Hi WingedPanther!

    Would you like to share the fun with us by giving the program for factorials, permutations, and combinations?
    Code:
    int factorial(int n){
      int fact=1;
      for (int i=1;i<=n;i++) fact*=i;
      return fact;
    }
    
    int permutation(int n,r){
      int perm=1;
      for (int i=n-r+1;i<=n;i++) perm*=i;
      return perm;
    }
    
    int combination(int n,r){
      return permutation(n,r)/factorial(r);
    }
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    samy is offline Newbie
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Re: Fibonacci with no recursion for fun

    Hi WingedPanther!

    Your code is awesome. I think you have a great mind.

    Good going.

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fibonacci with no recursion for fun

    It's a side-effect of being a mathematician: math algorithms come REALLY easily to me.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    samy is offline Newbie
    Join Date
    Jun 2008
    Posts
    13
    Rep Power
    0

    Re: Fibonacci with no recursion for fun

    Hi WingedPanther!

    I think you love mathematics. Have you tried some thing like derivatives or integrations, sine, cosine, etc.

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Fibonacci with no recursion for fun

    I've got a master's in math... yes, I love math
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. while loop for fibonacci series
    By jackson6612 in forum C and C++
    Replies: 14
    Last Post: 05-22-2011, 02:25 PM
  2. nth Fibonacci number using recursion
    By jackson6612 in forum C and C++
    Replies: 1
    Last Post: 05-21-2011, 03:31 PM
  3. Fibonacci difficult question
    By skd in forum C and C++
    Replies: 1
    Last Post: 04-24-2011, 12:34 AM
  4. Young–Fibonacci lattice
    By Tungsten Tide in forum C and C++
    Replies: 1
    Last Post: 03-19-2010, 10:20 AM
  5. Fibonacci Sequence
    By Termana in forum CSharp Tutorials
    Replies: 5
    Last Post: 07-27-2009, 12:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts