473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Fibonaci sequence

58
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
Expand|Select|Wrap|Line Numbers
  1. a, b = 0, 1
  2. while b < 1000:
  3. print b
  4.     a, b = b, a+b
  5.  
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.
Dec 23 '07 #1
1 1360
elcron
43
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
Expand|Select|Wrap|Line Numbers
  1. a, b = 0, 1
  2. while b < 1000:
  3. print b
  4.     a, b = b, a+b
  5.  
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
Expand|Select|Wrap|Line Numbers
  1. >>> def fib(maxNum=1000):
  2.     a, b = 0, 1
  3.     li = [b]
  4.     while b < 1000:
  5.         a, b = b, a+b
  6.         li.append(b)
  7.     return li
  8.  
  9. >>> fibNums = fib()
  10. >>> fibNums
  11. [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
  12. >>> for i in range(len(fibNums)):
  13.     print "Sequence %s: %s"%(i+1, fibNums[i])
  14. Sequence 1: 1
  15. Sequence 2: 1
  16. Sequence 3: 2
  17. Sequence 4: 3
  18. Sequence 5: 5
  19. Sequence 6: 8
  20. Sequence 7: 13
  21. Sequence 8: 21
  22. Sequence 9: 34
  23. Sequence 10: 55
  24. Sequence 11: 89
  25. Sequence 12: 144
  26. Sequence 13: 233
  27. Sequence 14: 377
  28. Sequence 15: 610
  29. Sequence 16: 987
  30. Sequence 17: 1597
  31. >>> def printSeq():
  32.     seqID = False
  33.     while not seqID:
  34.         seqID = raw_input("What sequence do you want? ")
  35.         if not seqID in [str(i) for i in range(1, len(fibNums) + 1)]: # +1 because range goes to max - 1
  36.             print "Must be a number in the range of 1-%s"%len(fibNums)
  37.             seqID = False
  38.         else:
  39.             seqID = int(seqID)
  40.     print "Sequence %s: %s"%(seqID, fibNums[seqID-1])
  41. >>> printSeq()
  42. What sequence do you want? elcron
  43. Must be a number in the range of 1-17
  44. What sequence do you want? 0
  45. Must be a number in the range of 1-17
  46. What sequence do you want? 18
  47. Must be a number in the range of 1-17
  48. What sequence do you want? 17
  49. Sequence 17: 1597
  50.  
Dec 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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...
5
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...
1
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,...
3
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...
8
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...
14
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
6
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...
1
davydany
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.