Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 8th, 2006, 4:46 PM   #1
baldy1324
Newbie
 
Join Date: Oct 2006
Location: texas
Posts: 3
Rep Power: 0 baldy1324 is on a distinguished road
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
baldy1324 is offline   Reply With Quote
Old Oct 8th, 2006, 5:02 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 8th, 2006, 7:39 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
~Moved
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 8th, 2006, 9:12 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 8th, 2006, 9:20 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 8th, 2006, 10:05 PM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 8th, 2006, 10:32 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
~Re-moved

(My appologies to the OP. Should have read the entire thread before moving it the first time.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower

Last edited by Mjordan2nd; Oct 9th, 2006 at 4:53 AM.
Mjordan2nd is offline   Reply With Quote
Old Oct 9th, 2006, 1:49 AM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 928
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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
titaniumdecoy is offline   Reply With Quote
Old Oct 9th, 2006, 3:54 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I'd be tempted to use a generator:
python Syntax (Toggle Plain Text)
  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 is offline   Reply With Quote
Old Oct 9th, 2006, 7:51 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Or perhaps:
python Syntax (Toggle Plain Text)
  1. x, y = 0, 1
  2. for i in xrange(1000):
  3. print "%d " % x,
  4. x, y = y, x + y
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
500 Ways to Print the Fibonacci Sequence uman Coder's Corner Lounge 19 Aug 25th, 2006 1:48 PM
fibonacci sequence angry_asian C++ 12 Aug 15th, 2006 3:53 AM
First Python Programme: Fibonacci Finder UnKnown X Python 2 Dec 15th, 2005 7:19 PM
Fibonacci lingon Python 8 Apr 29th, 2005 7:22 PM
Help - Execution Sequence anandt4u C++ 23 Apr 8th, 2005 4:46 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 AM.

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