...

View Full Version : list assignment out of range - (Fibonacci number generator)

KevinJohnson
01-28-2012, 09:46 AM
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

Calculated 0golden numbers
Calculated 1golden numbers
Calculated 2golden numbers
Traceback (most recent call last):
File "C:\Users\kevin\Desktop\fib.py", line 74, in <module>
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

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:
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:
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"

KevinJohnson
02-27-2012, 10:37 AM
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

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

Calculated 0golden numbers
Calculated 1golden numbers
Calculated 2golden numbers
Traceback (most recent call last):
File "C:\Users\kevin\Desktop\fib.py", line 74, in <module>
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

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:
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:
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"