TI-Basic Factor program

I've had lots of useful little programs on my TI-83 plus. Then it scrambles its memory and I have to figure them out again. Now it seems like it may have bitten the dust. I found Andie Graph to get a TI-83 on my phone.

One that I use most often is a naïve integer factorizer. After my calculator died, I've just had to re-implement it, so here it is.

The list ʟPRI is pre-filled with the first few primes. After checking those, the program checks all the odd numbers afterward. So ʟPRI must contain 2, and end with an odd prime (so that incrementing by 2 hits the odd numbers.)

You can just do {2,3}→ʟPRI and key in the FACTOR program. Or, you can use FILPRIMS to pre-compute the list of primes. The number 300 can be changed to any value of your choice, up to 999 (the maximum size of a list). But note that lists just stick around until you delete them, probably for years, and 999 primes is a major chunk of the calculator's memory, at least for a TI-83.

FILPRIMS
:SetUpEditor PRI
:If dim(ʟPRI)<2
:{2,3}→ʟPRI
:ʟPRI(dim(ʟPRI))→X
:While dim(ʟPRI)<300
:X+2→X
:2→J
:While ʟPRI(J)²≤X and fPart(X/ʟPRI(J))
:J+1→J
:End
:If ʟPRI(J)²>X
:Then
:Disp X
:X→ʟPRI(dim(ʟPRI)+1)
:End
:End
:SetUpEditor

Here, SetUpEditor PRI puts the list ʟPRI into the Stat→Edit view. It's used because it's the only way to bring a list into existence without clobbering it if it does exist. (Using dim on a non-existent list is an error.) SetUpEditor is called again at the end to restore the default Edit setup.

FACTOR
:Ans→X
:0→dim(ʟFAC)
:For(J,1,dim(ʟPRI))
:ʟPRI(J)→I
:0→K
:While not(fPart(X/I))
:K+1→K
:X/I→X
:I→ʟFAC(dim(ʟFAC)+1)
:End
:If K:Disp {I,K}
:If I²>X:Goto A
:End
:Repeat I²>X
:I+2→I
:0→K
:While not(fPart(X/I))
:K+1→K
:X/I→X
:I→ʟFAC(dim(ʟFAC)+1)
:End
:If K:Disp {I,K}
:End
:Lbl A
:If X>1:Then
:Disp {X,1}
:X→ʟFAC(dim(ʟFAC)+1)
:End
:ʟFAC

The program factors whatever is in Ans, the last result output. It displays each prime factor as it's found in a pair {p n}, where pn is the highest power of p dividing X. It also stores all the factors in ʟFAC, which is shown at the end, in case you missed some prime factors going by. So, prod(ʟFAC) gives you back the number; you can use this list to compute various divisor functions.