Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Classic - Fibonacci sequence

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars
Author Message
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sat Oct 02, 2004 11:52 pm    Post subject: Classic - Fibonacci sequence Reply with quote

Fibonacci sequence .. you all know it, do it how ever you wish. My example in Python takes an argument and returns the result of the sequence up to that argument.
   snip

Edited by: Algorithms  at: 10/3/04 8:58 am
Back to top
WannaBe
Wiggles


Joined: 22 Oct 2004
Posts: 714
Location: CA

PostPosted: Sun Oct 03, 2004 7:15 am    Post subject: Re: Classic - Fibonacci sequence Reply with quote

Quote:
you all know it

so...whats it doing?



www.000k.com/
WannaBe Games

Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sun Oct 03, 2004 7:56 am    Post subject: Re: Classic - Fibonacci sequence Reply with quote

If I would of posting the right code :) It sums the previous number using the result of the current sum. This is an example of a recursive sequence, obeying the simple rule that to calculate the next term one simply sums the preceding two...

OR

F(n) = F(n – 1) + F(n – 2)

0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
8 + 5 = 13
...

  # generatordef fib():  cur = 0  nxt = 1  while 1:    cur, nxt = nxt, cur + nxt    yield curfibout = fib()for i in range(10):  sys.stdout.write("%s " % fibout.next())

Edited by: Algorithms  at: 10/3/04 8:59 am
Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sun Oct 03, 2004 1:44 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

Might as well do CPP also...
Code:

#include <iostream>
using std::cout;
using std::endl;
void fib(int n) {
  // prints n in fibonacci sequence
  double cur = 0;
  double nxt = 1;
  for(int i = 0; i != n; i++) {
    double cpy = cur;
    cur = nxt;
    nxt = cur + cpy;
    cout << cur << " ";
  }
  cout << endl;
}

int main() {
  fib(10);
  return 0;
}


Last edited by Algorithms on Wed Jul 20, 2005 2:43 pm; edited 1 time in total
Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sun Oct 03, 2004 1:48 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

Output
 Python1 1 2 3 5 8 13 21 34 55 >>>C++wayne@dragon cpp $ ./fib1 1 2 3 5 8 13 21 34 55

Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sun Oct 03, 2004 1:54 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

And in case anyone is interested...
 wayne@dragon cpp $ time ./fib1 1 2 3 5 8 13 21 34 55real    0m0.006suser    0m0.010ssys     0m0.000swayne@dragon python $ time python fib.py1 1 2 3 5 8 13 21 34 55real    0m0.032suser    0m0.020ssys     0m0.010s

Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Sun Oct 03, 2004 2:00 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

I will resist the temptation to post this in other languages... I feel so alone... lol

Back to top
Cerulean
Gokenin


Joined: 22 Oct 2004
Posts: 742
Location: London, England

PostPosted: Mon Oct 04, 2004 10:06 am    Post subject: Re: Classic - Fibonacci sequence Reply with quote

This took me a solid 10 minutes to figure out (didn't want to look at your code and cheat :-P), i've never come across this before...

java script:
  function genSequence(n) {        var previous = 0;        var current = 1;        var tmp;        for(i = 1; i <= n; i++) {                document.write((previous + current) + "<br />");                tmp = current;                current = current + previous;                previous = tmp;        }}genSequence(10);


PHP:
  function genSequence($n) {        $previous = 0;        $current = 1;        $tmp;        for($i = 1; $i <= $n; $i++) {                echo(($previous + $current)."<br />");                $tmp = $current;                $current = $current + $previous;                $previous = $tmp;        }}genSequence(10);


both print:

1
2
3
5
8
13
21
34
55
89

Edited by: Cerulean Wave at: 10/4/04 11:08 am
Back to top
Blankety Blank Man
Ashigaru


Joined: 22 Oct 2004
Posts: 180

PostPosted: Tue Oct 05, 2004 6:17 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

Java:

 public class Fib{    public static void main(String[] args)    {        int n1 = 1;        int n2 = 1;        System.out.println(1 + "\n" + 1);        do        {            n2 = n1 + n2;            System.out.println(n2);            for(double a = 0; a < 1000000; a += 0.01);        }        while(1==1);    }}
Not really all that hard, you just need to know what the Fibbonacci sequence is

Back to top
Algorithms
Dragon


Joined: 21 Oct 2004
Posts: 343
Location: Florida

PostPosted: Wed Oct 06, 2004 6:53 am    Post subject: Re: Classic - Fibonacci sequence Reply with quote

Not really all that hard, you just need to know what the <insert any> is.. hehe

The great thing about the Fibonacci is when you first try it using Fib(n) = (Fibn-1) + Fib(n-2) the problem is very simple, but it is the temporary switching, that gets most people. Same with Linked Lists, the idea is very easy and one that is very simple to implement, but people have trouble with it until they see an example or finally it just clicks.

Sequences are weird like that.

Back to top
Eric
Idea Hamster


Joined: 21 Oct 2004
Posts: 679
Location: Bangor, Maine

PostPosted: Mon Oct 11, 2004 12:33 pm    Post subject: Re: Classic - Fibonacci sequence Reply with quote

www.programmers-corner.co...ib&=Search

It doesn't do the parameters thing... just the routine for the recursive calc... in VB6 and C/C++.



Eric D. Burdo - RLI Solutions



Sign Up Today for the Programmer's Corner Newsletter
the drop ship guide - The complete guide on to how to get started with wholesale drop shipping.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group