I've always thought it would be great to have a program that automatically shows large sequences of the Fibonacci sequence...so far i have -
a, b = 0, 1
-
while b < 1000:
-
print b
-
a, b = b, a+b
-
I want the program to show: "Sequence 1: 1". Sequence 2: "1", Sequence 3: "2", Sequence 4: "3", etc etc...
Does anybody know how I can do this? Also, I want the program to ask "What sequence do you wish to know?"
And then if i type, "61", it will show the 61th sequence of the Fibo. Series.
Thanks.
1 1360
I've always thought it would be great to have a program that automatically shows large sequences of the Fibonacci sequence...so far i have -
a, b = 0, 1
-
while b < 1000:
-
print b
-
a, b = b, a+b
-
I want the program to show: "Sequence 1: 1". Sequence 2: "1", Sequence 3: "2", Sequence 4: "3", etc etc...
Does anybody know how I can do this? Also, I want the program to ask "What sequence do you wish to know?"
And then if i type, "61", it will show the 61th sequence of the Fibo. Series.
Thanks.
you could store it in a list and use the index -
>>> def fib(maxNum=1000):
-
a, b = 0, 1
-
li = [b]
-
while b < 1000:
-
a, b = b, a+b
-
li.append(b)
-
return li
-
-
>>> fibNums = fib()
-
>>> fibNums
-
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
-
>>> for i in range(len(fibNums)):
-
print "Sequence %s: %s"%(i+1, fibNums[i])
-
Sequence 1: 1
-
Sequence 2: 1
-
Sequence 3: 2
-
Sequence 4: 3
-
Sequence 5: 5
-
Sequence 6: 8
-
Sequence 7: 13
-
Sequence 8: 21
-
Sequence 9: 34
-
Sequence 10: 55
-
Sequence 11: 89
-
Sequence 12: 144
-
Sequence 13: 233
-
Sequence 14: 377
-
Sequence 15: 610
-
Sequence 16: 987
-
Sequence 17: 1597
-
>>> def printSeq():
-
seqID = False
-
while not seqID:
-
seqID = raw_input("What sequence do you want? ")
-
if not seqID in [str(i) for i in range(1, len(fibNums) + 1)]: # +1 because range goes to max - 1
-
print "Must be a number in the range of 1-%s"%len(fibNums)
-
seqID = False
-
else:
-
seqID = int(seqID)
-
print "Sequence %s: %s"%(seqID, fibNums[seqID-1])
-
>>> printSeq()
-
What sequence do you want? elcron
-
Must be a number in the range of 1-17
-
What sequence do you want? 0
-
Must be a number in the range of 1-17
-
What sequence do you want? 18
-
Must be a number in the range of 1-17
-
What sequence do you want? 17
-
Sequence 17: 1597
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Anthony Best |
last post by:
I'm working on an idea that uses sequences.
I'm going to create a table like this:
id serial,
sequence int,
keyword varchar(32),
text text
for every keyword there will be a uniq sequence...
|
by: Eric E |
last post by:
Hi,
I have a question about sequences. I need a field to have values with
no holes in the sequence. However, the values do not need to be in order.
My users will draw a number or numbers from...
|
by: Marek Lewczuk |
last post by:
Hello,
I would like to ask if my problem with sequence is a proper behavior or
this is a bug (probably not)...
I have a table:
CREATE TABLE "testtable" (
"serialfield" SERIAL,...
|
by: kevin |
last post by:
Is that even possible?
I am creating a web service in .NET to expose some already created .NET
programs to other groups. One group is writing the client in PERL, and thus
wishes the wsdl schema...
|
by: regis |
last post by:
Greetings,
about scanf matching nonempty sequences using the "%" matches a nonempty sequence of anything except '-'
"%" matches a nonempty sequence of anything except ']" matches a nonempty...
|
by: pat270881 |
last post by:
hello,
I have to implement a sequence class, however the header file is
predefined
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
|
by: Defcon2030 |
last post by:
<bHey, can someone help me with this? I've been working on it for a
few days now, and my head's starting to spin... </b>
// FILE:ex1_imp.cxx
//
//
//
// CLASS IMPLEMENTED: sequence (see ex1.h...
|
by: davydany |
last post by:
Hey guys...a n00b Here for this site.
I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
|
by: Anan18 |
last post by:
Hello sir,
I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |