Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   fibonacci sequence (http://www.programmingforums.org/showthread.php?t=11522)

baldy1324 Oct 8th, 2006 3:46 PM

fibonacci sequence
 
hi i am diving into programming and wanting to find out the amount of time it takes to execute a fibonacci sequence program. i have had past expirience with c++ so i have that code. to make sure the way the program runs is the same, i would like it if this program used while loops, and other similar things. here is the c++ code -
:

#include <iostream>
using namespace std;

int main()
{
        int n = 1000;
        double first = 0;
        double second = 1;
        cout<<first<<"    "<<second<<"    ";

        while (n--)
                {
                        double third = first + second;
                        first = second;
                        second = third;
                        cout<<third<<"    ";
                }
}


so if anyone can "convert" this to python it would be appreciated

DaWei Oct 8th, 2006 4:02 PM

Please see your other thread. Somewhere on the way there or back, invest some time in showing enough consideration to read the rules/FAQ. Sheesh.

Mjordan2nd Oct 8th, 2006 6:39 PM

~Moved

bl00dninja Oct 8th, 2006 8:12 PM

a c++ while loop and a python one might have different big-O complexity. if you don't know what i mean by that...that's what you're asking so google it. nobody gives a rat shit about how much time it takes to execute anything...b/c that data is relative to processor specs, etc.

how long does it take to drive to new york?

see how vague that is? from where, how fast, how many stops, etc. that's why they have the big-O standard...it's absolute, not relative. certain alg's have specific growth rates.

did you write that code? i enjoy clever shit like while(i--), because it's short and weird...but it's probably not good nowadays when the length of a text file isn't going to screw up any memory issues...you have room to be clear and comment, please do so.

DaWei Oct 8th, 2006 8:20 PM

Complexity isn't everything. There are still plenty of applications, even on the desktop, where the difference in speed between C++ and a language like Python or Perl will be the difference between success and failure. It's why they have benchmarks.

bl00dninja Oct 8th, 2006 9:05 PM

yeah...that too. :)

you might look at the difference between different languages (as you may be doing already).

if i knew python i'd convert it for you, but i don't. i'll probably screw with it next summer.

use YOUR machine as an absolute reference and try different things...fib with loops, fib with recursion, etc. use different compilers and OS's if you can. this can be a pretty cool problem!

Mjordan2nd Oct 8th, 2006 9:32 PM

~Re-moved

(My appologies to the OP. Should have read the entire thread before moving it the first time.

titaniumdecoy Oct 9th, 2006 12:49 AM

:

n = 1000
first = 0.0
second = 1.0

print "%s    %s  " % (first, second),

while n != 0:
        third = first + second
        first = second
        second = third
        print "%s  " % third,
        n -= 1


Arevos Oct 9th, 2006 2:54 AM

I'd be tempted to use a generator:
:

  1. from itertools import islice
  2.  
  3. def fibonacci(x = 0, y = 1):
  4.     while True:
  5.         yield x
  6.         x, y = y, x + y
  7.  
  8. for i in islice(fibonacci(), 1000):
  9.     print "%d  " % i,


Arevos Oct 9th, 2006 6:51 AM

Or perhaps:
:

  1. x, y = 0, 1
  2. for i in xrange(1000):
  3.     print "%d  " % x,
  4.     x, y = y, x + y



All times are GMT -5. The time now is 3:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC