Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com list assignment out of range - (Fibonacci number generator)

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-28-2012, 10:46 AM PM User | #1 KevinJohnson Regular Coder   Join Date: May 2009 Location: China Posts: 133 Thanks: 1 Thanked 1 Time in 1 Post list assignment out of range - (Fibonacci number generator) I'm trying to create a Fibonacci number generator script that can resume from a previously stopped generation session. And i seem to be getting an error on the console. I'm new to python so, please be gentle ^__^ btw... once i have all of the bugs worked out of this basic script, i plan to integrate Parallel Python into this, so that really big Fibonacci numbers can be generated (up into the n = 1,000,000 or greater) in a reasonable amount of time. On a single thread, with an i7 (only using a single core) 720QM (1.6 Ghz) i seem to be able to generate 100,000 Fibonacci numbers within 2 days. That's a bit long to wait i think. Another area i plan to go with this is also integrating PyOpenCL, or PyCuda into my code base, so that speed can be further accelerated via GPU Here's the error i'm getting on the console Code: ```Calculated 0golden numbers Calculated 1golden numbers Calculated 2golden numbers Traceback (most recent call last): File "C:\Users\kevin\Desktop\fib.py", line 74, in fib(i) File "C:\Users\kevin\Desktop\fib.py", line 35, in fib terms[2] = line IndexError: list assignment index out of range``` here is my full code Code: ```import sys import os.path def fib(n): # Check to see if Phi.dat exists if os.path.exists('Phi.dat'): # Read the Phi.dat file and read the last 2 Golden Numbers # terms = [0,1] # Count the number of lines in the Phi.dat file Phifile = open("Phi.dat") linecount = 0 while 1: line = Phifile.readline() if not line: break pass # do something linecount = linecount + 1 MaxLines = linecount Phifile.close() # Set the last two Fibionacci numbers in Phi.dat to terms[0] and terms[1] Phifile = open("Phi.dat") linecount = 0 while 1: line = Phifile.readline() if not line: break pass # do something linecount = linecount + 1 if linecount == MaxLines -1: terms[1] = line if linecount == MaxLines: terms[2] = line Phifile.close() # Continue generating Fibonacci numbers until the specified limit has been reached # Open the Phi.dat file in append mode file = open('Phi.dat', 'a') i = 2 while i <= n: nth = str(terms[i-1] + terms[i-2]) terms.append(nth) i = i + 1 file.write(nth) file.close() else: # The file doesn't exist, so start generating Fibbonacci numbers from zero to the specified limit # If the file does not exist, set terms[0] = 0 and terms[1] = 1 terms = [0,1] # Open Phi.dat in the current directory in append mode file = open('Phi.dat', 'a') i = 2 while i <= n: nth = str(terms[i-1] + terms[i-2]) terms.append(nth) i = i + 1 file.write(nth) file.close() # Get the upper limit of Fibbonacci number to calculate limit = int(sys.argv[1]) # Let's makes sure that we didn't already generate the Fibonacci number specified on the command line for i in range(limit): fib(i) print "Calculated " + str(i) + "golden numbers"```
02-27-2012, 11:37 AM   PM User | #2
KevinJohnson
Regular Coder

Join Date: May 2009
Location: China
Posts: 133
Thanks: 1
Thanked 1 Time in 1 Post
Btw... if anyone wants to try calculating Fibonacci numbers, it's going to require a lot of memory (with larger numbers)...

My computer froze (ate all of it's memory) at about 4 GB.....

i got to the 250,000th Fibonacci number

Quote:
 Originally Posted by KevinJohnson I'm trying to create a Fibonacci number generator script that can resume from a previously stopped generation session. And i seem to be getting an error on the console. I'm new to python so, please be gentle ^__^ btw... once i have all of the bugs worked out of this basic script, i plan to integrate Parallel Python into this, so that really big Fibonacci numbers can be generated (up into the n = 1,000,000 or greater) in a reasonable amount of time. On a single thread, with an i7 (only using a single core) 720QM (1.6 Ghz) i seem to be able to generate 100,000 Fibonacci numbers within 2 days. That's a bit long to wait i think. Another area i plan to go with this is also integrating PyOpenCL, or PyCuda into my code base, so that speed can be further accelerated via GPU Here's the error i'm getting on the console Code: ```Calculated 0golden numbers Calculated 1golden numbers Calculated 2golden numbers Traceback (most recent call last): File "C:\Users\kevin\Desktop\fib.py", line 74, in fib(i) File "C:\Users\kevin\Desktop\fib.py", line 35, in fib terms[2] = line IndexError: list assignment index out of range``` here is my full code Code: ```import sys import os.path def fib(n): # Check to see if Phi.dat exists if os.path.exists('Phi.dat'): # Read the Phi.dat file and read the last 2 Golden Numbers # terms = [0,1] # Count the number of lines in the Phi.dat file Phifile = open("Phi.dat") linecount = 0 while 1: line = Phifile.readline() if not line: break pass # do something linecount = linecount + 1 MaxLines = linecount Phifile.close() # Set the last two Fibionacci numbers in Phi.dat to terms[0] and terms[1] Phifile = open("Phi.dat") linecount = 0 while 1: line = Phifile.readline() if not line: break pass # do something linecount = linecount + 1 if linecount == MaxLines -1: terms[1] = line if linecount == MaxLines: terms[2] = line Phifile.close() # Continue generating Fibonacci numbers until the specified limit has been reached # Open the Phi.dat file in append mode file = open('Phi.dat', 'a') i = 2 while i <= n: nth = str(terms[i-1] + terms[i-2]) terms.append(nth) i = i + 1 file.write(nth) file.close() else: # The file doesn't exist, so start generating Fibbonacci numbers from zero to the specified limit # If the file does not exist, set terms[0] = 0 and terms[1] = 1 terms = [0,1] # Open Phi.dat in the current directory in append mode file = open('Phi.dat', 'a') i = 2 while i <= n: nth = str(terms[i-1] + terms[i-2]) terms.append(nth) i = i + 1 file.write(nth) file.close() # Get the upper limit of Fibbonacci number to calculate limit = int(sys.argv[1]) # Let's makes sure that we didn't already generate the Fibonacci number specified on the command line for i in range(limit): fib(i) print "Calculated " + str(i) + "golden numbers"```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:36 AM.