Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-15-2006, 10:40 PM   #1 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 12

techno_kid

Default program for fibonacci?

how will you form an algorithm/program ( any language ) to print the fibonacci numbers?

thanks for the help.. =)
techno_kid is offline  
Old 01-16-2006, 08:20 AM   #2 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 11

snif innards

Default

a simple version:
PHP Code:
<?
$x 
0;
$y 1;
for(
$i=0$i<20$i++){
$z $x $y;
$x=$y;
$y=$z;
echo 
$z."
\n"
;
}
?>

snif innards is offline  
Old 01-17-2006, 04:25 AM   #3 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 12

techno_kid

Default

oh thanks snif... =)

any other example there guys ?

thanks =)
techno_kid is offline  
Old 01-24-2006, 03:07 PM   #4 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default

this program is in C language. hope you find it useful.

//program for fibonacci numbers
#include<stdio.h>
int main()
{
int old,cur,new,i,num;
printf("\nEnter the number of iterations you want to be performed: ");
scanf("%d",&num);
old=new=0;
cur=1;
printf("the fibonacci series is....\n");
printf("\t%d",old);
printf("\t%d",cur);
for(i=2;i<=num;i++)
{
new=old+cur;
printf("\t%d",new);
old=cur;
cur=new;
}
return 0;
}
simple is offline  
Old 01-24-2006, 09:53 PM   #5 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 12

techno_kid

Default

thanks simple...

this would surely help me... =)
techno_kid is offline  
Old 01-24-2006, 09:57 PM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Another simple method for finding the n-th fibbonacci number using recursion:

Code:
int fib(int n)
{
   if (n == 1) return 1;
   if (n == 2) return 1;
   return fib(n-1) + fib(n-2);
}

__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 01-24-2006, 10:05 PM   #7 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 12

techno_kid

Default

oh thanks TheHeadFL..

this is pretty short and simple.. =)
techno_kid is offline  
Old 01-24-2006, 11:56 PM   #8 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Keep in mind though that using that method to print out a list of all the fibbonacci numbers would be extremely inefficient since each time you call it it generates a lot of function calls. (It calls itself approximately 2^n times, give or take)

If you try to calculate fib(1000) or something, you might overflow the stack.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
 
Closed Thread

Thread Tools
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