Prime factorization program (TI-84+)

5.2k views Asked by At

I'm making a prime factorization program on my calculator. It works fine for smaller numbers, but it's showing strange behavior for 2^n, n≥47. It'll do fine for a while, but then at some point the program breaks down and after spitting out the prime numbers 17 and 353, keeps running forever.

With my extremely limited knowledge in programming, I'm suspecting that the calculator can't handle such a large number accurately and messes up the program.

Here's the code: (variables explained below; outputs prime factors in the form of A+Bi for a prime factor of A^B)

ClrHome
Disp "N=Number
Input "N: ",N
If N≥2 and not(fPart(N)):Then
    0→dim(ʟP)
    2→I
    0→R
    0→S
    Repeat N=1
        If not(fPart(N/I)):Then
            While not(fPart(N/I))
                N/I→N
                S+1→S
            End
            R+1→R
            I+Si→ʟP(R)
            0→S
        End
        I+1→I
    End
End
ʟP

#N: Number to be prime-factored (input)

#I: A prime factor

#R: Number of unique prime factors

#S: Exponent on prime factor

#ʟP: Prime factorization of N (output)

To reproduce this problem, run the program and input any 2^n for n≥47.

Anyone know why this is happening?

1

There are 1 answers

2
Timtech On BEST ANSWER

Your hypothesis is correct. A real number data type in TI-Basic can only store up to 44 bits of data (source). This can be proved by using the solve( command in the following way:

solve((X+1)-X,X,0

Whether run as a program, called as a subprogram, or executed from the home screen, this program/function will output a large number in a similar range as your failing point (which should never happen because X+1 would never be equal to X).

Also, I shortened your code down a bit:

ClrHome
Prompt N
If N≥2 and not(fPart(N:Then
    0→dim(ʟP
    DelVar RDelVar S2→I
    Repeat N=1
        If not(fPart(N/I:Then
            While not(fPart(N/I
                N/I→N
                S+1→S
            End
            R+1→R
            I+Si→ʟP(R
            0→S
        End
        I+1→I
    End
End
ʟP