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 11-15-2007, 11:35 PM
shinta's Avatar   
shinta shinta is offline
Newbie
 
Join Date: Nov 2007
Location: Austin, TX
Posts: 7
Rep Power: 0
shinta is on a distinguished road
Send a message via AIM to shinta Send a message via MSN to shinta
Smile Fibonacci's number

hi... well its been plauging me.

i know the formula is this

n = 0 its 0
n = 1 its 1

if n > 1 than

(n-1) + (n-2)


so.. im just having a brain block in making an algorithm out of it. im new to programming so im just trying to challenge my self =D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-16-2007, 07:22 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

C Code:
  1. int fib(int x) {
  2.     if((x == 0) || (x == 1)) {
  3.         return x;
  4.     }
  5.     return fib(x - 1) + fib(x - 2);
  6. }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-16-2007, 07:25 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

You might want to assert(x >= 0) at the start of the function.

This isn't very efficient either but is the direct recursive translation of the function.

You can improve that with either a hash table storing known solutions or by switching to an iterative version. The first isn't easy to pull off nicely in C (lack of closures or objects) but the second can be done.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-19-2007, 12:43 AM
mokszyk's Avatar   
mokszyk mokszyk is offline
Newbie
 
Join Date: Oct 2007
Posts: 11
Rep Power: 0
mokszyk is on a distinguished road
Default

my try
cpp Code:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fib(int n) // short version
  5. {
  6.     if (n==1 || n==2)
  7.     {
  8.              return 1;
  9. }
  10. else
  11. {
  12.         return fib(n-2) + fib(n-1);
  13. }       
  14. }
  15.  
  16. int main()
  17. {
  18.   int wej;
  19.   cout << "Ktory wyraz ciagu Fibonacciego podac?\n"; //choose what number
  20.   cin >> wej;
  21.   cout << fib(wej) << "\n";
  22.   system("pause"); //ewentualy if u want see pause
  23.   return 0;
  24. }
__________________
if i helped you, please press "thanks" I will apreciate it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-19-2007, 01:39 AM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,477
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

My three solutions

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

Last edited by John; 11-19-2007 at 02:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 11-26-2007, 05:50 PM
shinta's Avatar   
shinta shinta is offline
Newbie
 
Join Date: Nov 2007
Location: Austin, TX
Posts: 7
Rep Power: 0
shinta is on a distinguished road
Send a message via AIM to shinta Send a message via MSN to shinta
Default

thanks guys i apreciate it =D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-26-2007, 07:05 PM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 19
Posts: 113
Rep Power: 4
gszauer is on a distinguished road
Default

This article might be a bit of help:
Fibonacci Numbers
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-28-2007, 03:41 AM
codecall33 codecall33 is offline
Newbie
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
codecall33 is on a distinguished road
Default

Hello friend !
I will say thanks to all the member for this job
it is really very nice job and i am also try to help to all the member.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-02-2007, 04:49 PM
jbakid jbakid is offline
Newbie
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
jbakid is on a distinguished road
Default Fibbonacci

Here's one (assumes n > 0):
C Code:
  1. [font="Courier New"]
  2. int fibbonacci(int n){
  3.   return n<2?1:fibbonacci(n-1)+fibbonacci(n-2);
  4. }[/font]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
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
if text1.text = number ikkeugh Visual Basic Programming 2 10-21-2007 11:04 AM
How to calculate the number of pixels in a polygon extreme Java Help 5 04-17-2007 11:34 AM
Number Cloning Ronin Java Help 3 12-21-2006 08:23 AM
I need to generate a random number in PHP dirkfirst PHP Forum 7 07-05-2006 10:58 PM
Number of files and directories in folder dirkfirst PHP Forum 4 05-30-2006 11:06 AM


All times are GMT -5. The time now is 03:32 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads