Post Reply 
Help With Python?
07-17-2024, 05:38 PM (This post was last modified: 07-17-2024 05:40 PM by toml_12953.)
Post: #1
Help With Python?
I'm trying to convert the following ANSI BASIC program to Python. The BASIC program works but the Python program gets a bad index error after some time. I've looked at it and looked at it but I can't discover what's causing the problem. Can anyone help?

BASIC Program:
Code:
100 LET T0=TIME
110 LET Y=30000
120 DIM List1(30000)
130 LET L=0
140 FOR N=1 TO 999999
150    LET M=N
160    LET C=0
170    DO WHILE M<>1
180       IF FP(M/2)=0 THEN
190          LET M=M/2
200       ELSE
210          LET M=3*M+1
220       END IF
230       LET C=C+1
240       IF M<=Y THEN
250          IF List1(M)<>0 THEN
260             LET C=C+List1(M)
270             LET M=1
280          END IF
290       END IF
300    LOOP
310    IF N<=Y THEN
320       IF List1(N)=0 THEN
330          LET List1(N)=C
340       END IF
350    END IF
360    IF C>L THEN 
370       LET L=C
380       PRINT N
390       PRINT C
400    END IF
410 NEXT N
420 PRINT TIME-T0;"seconds"
430 END

Python Program:
Code:
from time import *

def Euler14():

    t0=monotonic()

    Y = 30000
    List1 = [0] * Y
    L = 0
    for N in range(1,1000000):
        M = N
        C = 0
        while M != 1:
            if M % 2 == 0:
                M = int(M / 2)
            else:
                M = 3 * M + 1
            C = C + 1
            if M <= Y:
                if List1[M] != 0:
                    C = C + List1[M]
                    M = 1
        if N <= Y:
            if List1[N] == 0:
                List1[N] = C
        if C > L:
            L = C
            print(N)
            print(C)
        
    print(monotonic()-t0,"seconds")

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
07-17-2024, 05:52 PM (This post was last modified: 07-17-2024 05:52 PM by ldesnogu.)
Post: #2
RE: Help With Python?
I’m not very familiar with Python but I guess list has 30000 elements indexed from 0 to 29999. So either you should dimension it to Y+1 or make comparisons <Y rather than <=Y.

Just guessing Smile
Find all posts by this user
Quote this message in a reply
07-17-2024, 07:07 PM
Post: #3
RE: Help With Python?
Another alternative to ldesnogu approach is to use an AI to convert the code.
This gives this which seem to work. At least it runs without an error.

Code:

import time

# Initialize variables
t0 = time.time()
Y = 30000
List1 = [0] * (Y + 1)
L = 0

# Loop from 1 to 999999
for N in range(1, 1000000):
    M = N
    C = 0
    while M != 1:
        if M % 2 == 0:
            M = M // 2
        else:
            M = 3 * M + 1
        C += 1
        if M <= Y:
            if List1[M] != 0:
                C += List1[M]
                M = 1
    if N <= Y:
        if List1[N] == 0:
            List1[N] = C
    if C > L:
        L = C
        print(N)
        print(C)

print(f"{time.time() - t0} seconds")
Find all posts by this user
Quote this message in a reply
07-18-2024, 03:30 AM
Post: #4
RE: Help With Python?
(07-17-2024 05:52 PM)ldesnogu Wrote:  I’m not very familiar with Python but I guess list has 30000 elements indexed from 0 to 29999. So either you should dimension it to Y+1 or make comparisons <Y rather than <=Y.

Just guessing Smile

Quite right! Thanks!

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)