Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-2008, 04:27 AM
HAL 9000 HAL 9000 is offline
Newbie
 
Join Date: Jun 2008
Posts: 17
Rep Power: 2
HAL 9000 is on a distinguished road
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 04:32 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-23-2008, 07:36 AM
samy samy is offline
Newbie
 
Join Date: Jun 2008
Posts: 13
Rep Power: 0
samy is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-23-2008, 12:03 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Fibonacci with no recursion for fun

I tend to do factorials, permutations, and combinations in loops for the same reason.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-24-2008, 07:21 AM
samy samy is offline
Newbie
 
Join Date: Jun 2008
Posts: 13
Rep Power: 0
samy is on a distinguished road
Default 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?
__________________
My favorite place: http://gallery.techarena.in/showphoto.php/photo/9260
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-24-2008, 07:50 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,478
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default 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:

C++ Code:
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. //Non-recrusive: O(1)
  8. unsigned long fib(int n)
  9. {
  10.      return (1/sqrt(5)) * (pow(((1 + sqrt(5)) / 2), n) - pow(((1 - sqrt(5)) / 2), n));
  11. }
  12.  
  13. //Recrusive: O(n)
  14. unsigned long fib2(int n)
  15. {
  16.     if(n <= 2) return 1;
  17.     else return fib2(n - 1) + fib2(n - 2);
  18. }
  19.  
  20. //Iterative: O(n)
  21. unsigned long fib3(int n)
  22. {
  23.     int u = 0;
  24.     int v = 1;
  25.     int i, t;
  26.  
  27.     for(i = 2; i <= n; i++)
  28.     {
  29.         t = u + v;
  30.         u = v;
  31.         v = t;
  32.     }
  33.     return v;       
  34. }
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 06-24-2008, 01:27 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default 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);
}
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-25-2008, 07:53 AM
samy samy is offline
Newbie
 
Join Date: Jun 2008
Posts: 13
Rep Power: 0
samy is on a distinguished road
Default Re: Fibonacci with no recursion for fun

Hi WingedPanther!

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

Good going.
__________________
My favorite place: http://gallery.techarena.in/showphoto.php/photo/9260
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-25-2008, 01:06 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Fibonacci with no recursion for fun

It's a side-effect of being a mathematician: math algorithms come REALLY easily to me.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-26-2008, 08:27 AM
samy samy is offline
Newbie
 
Join Date: Jun 2008
Posts: 13
Rep Power: 0
samy is on a distinguished road
Default 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.
__________________
My favorite place: http://gallery.techarena.in/showphoto.php/photo/9260
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 06-26-2008, 11:41 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Fibonacci with no recursion for fun

I've got a master's in math... yes, I love math
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tail recursion Chinmoy C Tutorials 12 06-15-2008 10:49 AM
Help with recursion { recursive helper function } mibit C and C++ 3 05-18-2008 08:37 AM
recursion Chinmoy C Tutorials 0 03-19-2008 12:57 AM
need help drawing Sierpinski triangles using recursion dalearyous Java Help 3 09-18-2007 01:55 PM
Recursion Problem cooldude C and C++ 3 11-29-2006 12:42 PM


All times are GMT -5. The time now is 11:24 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75