Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    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 <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

    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"

  • #2
    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 View Post
    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 <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

    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"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •