![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Location: texas
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
~Moved
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
~Re-moved
(My appologies to the OP. Should have read the entire thread before moving it the first time.
__________________
"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." - Dwight D. Eisenhower Last edited by Mjordan2nd; Oct 9th, 2006 at 4:53 AM. |
|
|
|
|
|
#8 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I'd be tempted to use a generator:
python Syntax (Toggle Plain Text)
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Or perhaps:
python Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |