473,242 Members | 1,472 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,242 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 1357
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.