|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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. |
| Sponsored Links |
|
|
|
|||
|
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. |
|
|||||
|
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. |
|
|||
|
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 |
|
|||||
|
Quote:
C++ Code:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
|
|||||
|
Quote:
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. |
|
|||
|
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 |
|
|||||
|
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. |
|
|||
|
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 |
|
|||||
|
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. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |