| Author |
Message |
Algorithms Dragon
Joined: 21 Oct 2004 Posts: 343 Location: Florida
|
|
| Back to top |
|
 |
WannaBe Wiggles
Joined: 22 Oct 2004 Posts: 714 Location: CA
|
Posted: Sun Oct 03, 2004 7:15 am Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
|
| Back to top |
|
 |
Algorithms Dragon
Joined: 21 Oct 2004 Posts: 343 Location: Florida
|
Posted: Sun Oct 03, 2004 1:44 pm Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
Posted: Sun Oct 03, 2004 1:48 pm Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
Posted: Sun Oct 03, 2004 1:54 pm Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
Posted: Sun Oct 03, 2004 2:00 pm Post subject: Re: Classic - Fibonacci sequence |
|
|
| 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
|
Posted: Mon Oct 04, 2004 10:06 am Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
Posted: Tue Oct 05, 2004 6:17 pm Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
Posted: Wed Oct 06, 2004 6:53 am Post subject: Re: Classic - Fibonacci sequence |
|
|
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
|
|
| Back to top |
|
 |
|