TI-85/6 Programs
The TI-85/6 graphing calculators are powerhouses in the the \$100 hand-held computer category.  These programs were written on the '85, but the '86 should run the same code.
Contents:
• BAS26 converts a base 26 alphabetic number to base 10
• EALG implements the Euclidean algorithm to find

• the greatest common divisor of two numbers
• EUCL is an improvement on EALG
• JOSEP2 finds the survivor of Flavius Josephus' problem:  If N objects are arranged in a circle and as you count around the circle every kth one is removed, what is the original position of the survivor?
• JOS5 is a vast improvement on how to solve the Josephus problem.
• MBAS26 multiplies two base 26 numbers and writes the

• in base 26 alphabetic characters
• MODU solves a modular equation of the form

• A*N = B mod M   for N.
• QE2 is a program for computing, in binary mode, x^z mod N

• using successive multiplication.
• PRI is a program for factoring numbers whose smallest

• prime factors are at most 2671 (and counting)

EALG implements the Euclidean algorithm to find the greatest common divisor of two numbers.
:ClLCD
:Outpt(5,1,"This program will")
:Outpt(6,1,"find the greatest")
:Outpt(7,1,"common divisor of")
:Outpt(8,1,"two integers.")
:Input "Larger integer:",A
:Input "Smaller integer:",B
:0→i
:A→U :B→V
:ClLCD :While (V>0)
:i+1→i
:iPart (U/V)→q
:ClLCD
:Outpt(6,1,"quotient = ")
:Outpt(7,1,q)
:V→t :U-q*V→V
:t→U
:ClLCD
:Outpt(3,1,"i = ")
:Outpt(3,5,i)
:Outpt(4,1,"remainder = ")
:Outpt(5,1,V)
:Pause
:End
:ClLCD
:Outpt(4,1,"The g.c.d. is")
:Outpt(5,1,U)

EUCL is an improvement of EALG
:ClLCD
:Outpt(5,1,"This program will")
:Outpt(6,1,"find the greatest")
:Outpt(7,1,"common divisor of")
:Outpt(8,1,"two integers.")
:Input "Larger integer:",A
:Input "Smaller integer:",B
:0→i :A→U
:B→V
:ClLCD
:While (V≠0)
:i+1→i
:mod(U,V)→R
:V→U
:R→V
:ClLCD
:Outpt(3,1,"i = ")
:Outpt(3,5,i)
:Outpt(4,1,"remainder = ")
:Outpt(5,1,V)
:Pause
:End
:ClLCD
:Outpt(4,1,"The g.c.d. is")
:Outpt(5,1,U)

Josep2 is a program for finding the survivor of the the Flavius Josephus problem: If N objects are arranged in a circle and as you count around the circle every kth one is removed, what is the original position of the survivor? Note that this code is *really* inefficient!  For a thorough discussion of this problem, go to the Josephus page.

:ClLCD
:Input "Number in circle?",N
:Input "Remove every?",K
:N→dimL S     // S will hold all the original objects.
:For(I,1,N)   // The objects are initialized in the form of a complex number
:(I,0)→S(I) // where the real part is the number and the imaginary part is the
:End                  // condition: dead or alive.
:1→I // I counts the number of objects (living or dead) encountered in all.
:1→M      // M is the number of objects encountered.
:0→D       // D is the number of objects killed so far.
:While (D<N-1)
If ((mod(M,K)==0) and (imag S(1+mod(I-1,N))==0))
// If your time is up and you're still alive . . .
:  Then
:    (1+mod(I-1,N),1)→S(1+mod(I-1,N))   // Bye, bye - you're removed.
:    D+1→D                 // Increase the number of dead.
I+1→I             // Increase the number of living or dead encountered.
:    M+1→M           // Increase the number of living encountered.

:For(J,0,N-1)
:Outpt(1+mod(J,8),1,S(J+1))
:If (((mod(J+1,8)==0) or (mod(J+1,N)==0)))  // This code is optional - it will print
:Then                                       // out the players, living and dead
:Pause
:End
:End

:  End
//  If a living object is encountered whose number is not up
:  If (imag S(1+mod(I-1,N))==0)
:  Then
:    M+1→M   // Increase the number of living encountered.
:  End       // End "if" of  "alive and number's up" condition
:  I+1→I   // In any case, increase the number of living or dead encountered.
:End       // End while
:0→J
:Repeat (imag (S(J))==0)   // Find the number of the survivor
:  J+1→J
:End
:Outpt(4,7,"The number of")
:Outpt(5,7,"the survivor ")
:Outpt(6,7,"is")
:Outpt(6,10,real (S(J))

JOS5 is a program for finding the survivor of the the Flavius Josephus problem: If N objects are arranged in a circle and as you count around the circle every kth one is removed, what is the original position of the survivor? Note that this code is *really* efficient!  For a thorough discussion of this problem, go to the Josephus page.

:ClLCD
:Outpt(3,1,"Enter the number, N,"
:Outpt(4,1,"of starters"
:Outpt(5,1,"The number,K,to omit."
:Outpt(6,1,"& the I-th eliminated"
:Prompt N,K,I
:I*Küx
:While (x>N)
:iPart ((K*(x-N)-1)/(K-1))üx
:End
:Outpt(7,1,"The"
:Outpt(7,5,I)
:Outpt(7,6+iPart (log (I)),"-th one out is"
:Outpt(8,1,x)

MODU is a program to solve a modular equation
of the form   A*N = B mod M   for N.
:ClLCD
:Outpt(4,5,"Solve")   //Display intro screen
:Outpt(5,1, "A*N = B (mod M)")
:Outpt(6,5,"for N.")
:Input "A = ", A      // Input given values
:Input "B = ",B
:Input "M = ",M
:mod(A,M)
→C     // Reduce A mod M  to make computation simpler
:For(N,1,M-1,1)     // This looping could be made more efficient
:If (fPart ((C*N-B)/M)==0)     // Check to see if M divide C*N-B
:Then
:iPart(log(A))üP       // Make the output screen pretty
:Outpt(5,1,A)
:Outpt(5,2+P,"*")
:Outpt(5,3+P,N)
:Outpt(6,2,"=")
:Outpt(6,4,B)
:iPart(log(B))→P
:Outpt(6,6+P,"mod")
:Outpt(6,10+P,M)
:Stop
:End
:End

QE2 is a program for computing x^z mod N using
the square and multiply method in binary mode.

:Dec              // Set to decimal mode
:ClLCD
:Outpt(4,1,"This program computes")     // Display intro screen
:Outpt(5,5,"x^z (mod N)")
:Outpt(6,2,"using successive")
:Outpt(7,3,"multiplications")
:Prompt x,z,N     // Input parameters
:Bin          // Set to binary mode
:1→s       // Initialize s. s will hold the final result.
:x→t       // Keep x and z
:z→w       // for use at end.
:While (w≠0)  // BIG LOOP. When w is finally right shifted away, we're done.
:  Disp "s=",såDec  //
:  Disp "t=",tåDec  // For debugging
:  Disp "w=",wåDec  // or just watching
:  Pause            //
:  If (w and 1) //   If what's currently in the exponent, w, is odd
:    mod(s*t,N)üs //Then Multiply current result s by current t and reduce mod N
:  shftR w→w    // Shift the exponent right 1 each iteration of BIG LOOP.
:  mod(t*t,N)→t // Replace the current t by the square of t, reduced mod N.
:End          // End While. (BIG LOOP)
:Dec  // Reset decimal mode
:ClLCD
:Outpt (2,3,"x^z (modN) with") // Present results.
:Outpt (3,4,"x =")
:Outpt (3,8,x)
:Outpt (4,4,"z =")
:Outpt (4,8,z)
:Outpt (5,4,"N =")
:Outpt (5,8,N)
:Outpt (7,3,"is ")
:Outpt (7,6,s)

PRI is a program for factoring numbers whose smallest
prime factors are at most 2671 (and counting).
You'll also need the list of primes: p.uue.
:0→K //initialize the prime factor counter
:Input "ENTER N ",N   //Enter the number to be factored
:1→I // initialize the prime counter
:While (P(I)ò÷N) // BIG LOOP while ith prime's square ÷ N
:  If (mod(N,P(I))==0) // If ith prime divides N
:  Then
:    1→J // J will count to the largest power of p(I) that divides N
:    While (mod(N,(P(I))^(J+1))==0) // While a higher power divides N
:      J+1→J // increment power
:    End // End nested while
:    K+1→K // increment number of distinct prime divisors.
:    K→dimL F    // F will be a list of prime power divisors p^k
:    (I,J)→F(K)  // with real part = p and imaginary part = k.
:    N/(P(I)^J)→N // Simply factor the remaining factor
:  End // End If
:  I+1→I // Increment prime counter
:End   // (BIG LOOP) End while since ith prime's square ù N
:ClLCD
:If (N≠1) // If there are proper divisors
:  Outpt(1,1,N) // This will be remaining (perhaps unfactored) factor
:For(M,1,K) // Run through list of prime divisors
:  Outpt(1+mod(M,9),1,P(real F(M))) // Fancy typesetting, eh?
:  Outpt(1+mod(M,9),2+int (log P(real F(M)))," ^ ") :  Outpt(1+mod(M,9),5+int (log P(real F(M))),imag F(M))
:  Pause // If there are lots of distinct prime factors
:End // End For loop

BAS26
This program will convert a base 26 alphabetic
number to a base 10 digital number.
// Dimension a list, A(), to hold the alphabet symbols
:26→dimL A
// Write the alphabet set to list A(). (See next program for an easier way to do this.)
:"A"→A(1) :"B"→A(2) :"C"→A(3) :"D"→A(4) :"E"→A(5) :"F"→A(6) :"G"→A(7) :"H"→A(8) :"I"→A(9) :"J"→A(10) :""K→A(11) :"L"→A(12) :"M"→A(13) :"N"→A(14) :"O"→A(15) :"P"→A(16) :"Q"→A(17) :"R"→A(18) :"S"→A(19) :"T"→A(20) :"U"→A(21) :"V"→A(22) :"W"→A(23) :"X"→A(24) :"Y"→A(25) :"Z"→A(26)
// n will hold the number of characters in a factor
:0→N
// next is a boolean variable to control the Repeat loop
:1→next
:Repeat next
// Wait for a keypress
:While (getKy==0)
:End
// A key has been pressed. Put the keypress value in k
:getKy→K
// If the keypress value is between 51 and 55 (A-E on the TI)
:If (K≥51 and K≤55)
:Then
// Increment the character counter.
:N+1→N
// Show the character on the screen
:Outpt(5,N+1,A(K-50))   // Dimension the first factor to accept a new character
:N→dimL F1
// Write the numerical value of that character into the list for the first factor
:k-51→F1(n)
// You've already got a character, so don't get another without keypress
:Goto skip
:End
:If (K≥61 and K≤65)
// Same as other loops 'cept for letters F - J
:Then
:N+1→N
:Outpt(5,N+1,A(K-55))
:N→dimL F1
:K-56→F1(N)
:Goto skip
:End
:If (K≥71 and K≤75) // Letters K - O
:Then
:N+1→N
:Outpt(5,N+1,A(K-60))
:N→dimL F1
:K-61→F1(N)
:Goto skip
:End
:If (K≥81 and K≤85) // Letters P -T
:Then
:N+1→N
:Outpt(5,N+1,A(K-66))
:N→dimL F1
:K-67→F1(N)
:Goto skip
:End
:If (K≥92 and K≤95) // Letters U - X
:Then
:N+1→N
:Outpt(5,N+1,A(K-71))
:N→dimL F1
:K-72→F1(N)
:Goto skip
:End
:If (K≥102 and K≤103) // Letters Y and Z
:Then
:N+1→N
:Outpt(A(K-79),5,N+1)
:N→dimL F1
:K-78→F1(N)
:Goto skip
:End
:if(K==105) // Exit repeat loop when ENTER is pressed
:0→next
:skip // DESTINATION FOR SKIPS ABOVE
:End
:Outpt(5,N+2,"=") // Show equal sign on screen as part of equation
// N will hold the base 10 value of the base 26 number entered
:0→M
:For(i,1,N)
// Compute the base 10 value of the number entered in base 26
:F1(i)*26^(N-i)+M→M
:End
:Outpt(M,6)

MBAS26
Multiply two base 26 numbers
to produce a base 26 output.
This uses features of the previous code
and improves on some.
:ClLCD
// This is a much quicker way to to enter the list of alphabetic characters
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"→A
:0→N
:1→next
// To see how this works, take a look at the annotation for above.
// What's new here is just a repetition of input for each of 2 base 26
// factors and a computation of the product translated to base 26 at the end.
:Outpt(1,1,"Enter first factor")
:Outpt(2,1,"Press ENTER to finish")
:While (next==1)
:0→IKY
:Repeat (IKY≠0)
:getKy→IKY
:End
:If (IKY≥51 and IKY≤55)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-50,1))
:N→dimL F1
:IKY-51→F1(N)
:End
:If (IKY≥61 and IKY≤65)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-55,1))
:N→dimL F1
:IKY-56→1(N)
:End
:If (IKY≥71 and IKY≤75)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-60,1))
:N→dimL F1
:IKY-61→F1(N)
:End
:If (IKY≥81 and IKY≤85)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-65,1))
:N→dimL F1
:IKY-66→F1(N)
:End
:If (IKY≥92 and IKY≤95)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-71,1))
:N→dimL F1
:IKY-72→F1(N)
:End
:If (IKY≥102 and IKY≤103)
:Then
:N+1→N
:Outpt(3,N+1,sub(A,IKY-77,1))
:N→dimL F1
:IKY-78→F1(N)
:End
:If (IKY==105)
:Then
:0→next
:End
:End
:Outpt(3,N+2,"=")
:0→P
:For(i,1,N)
:P+F1(i)*26^(N-i)→P
:End
:Outpt(3,N+3,P)
:
:0→N
:1→next
// Repeat above input of base 26 factor for second factor
:Outpt(4,2,"Enter 2nd factor.")
:Outpt(5,1,"Press ENTER to finish")
:While (next==1)
:0→IKY
:Repeat (IKY≠0)
:getKy→IKY
:End
:If (IKY≥51 and IKY≤55)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-50,1))
:N→dimL F1
:IKY-51→F1(N)
:End
:If (IKY≥61 and IKY≤65)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-55,1))
:N→dimL F1
:IKY-56→F1(N)
:End
:If (IKY≥71 and IKY≤75)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-60,1))
:N→dimL F1
:IKY-61→F1(N)
:End
:If (IKY≥81 and IKY≤85)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-65,1))
:N→dimL F1
:IKY-66→F1(N)
:End
:If (IKY≥92 and IKY≤95)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-71,1))
:N→dimL F1
:IKY-72→F1(N)
:End
:If (IKY≥102 and IKY≤103)
:Then
:N+1→N
:Outpt(6,N+1,sub(A,IKY-77,1))
:N→dimL F1
:IKY-78→F1(N)
:End
:If (IKY==105)
:Then
:0→next
:End
:End
:Outpt(6,N+2,"=")
// Q holds the second factor
:0→Q
:For(i,1,N)
:Q+F1(i)*26^(N-i)→Q
:End
:Outpt(6,N+3,Q)
:
:Outpt(7,1,"Product =")
// Compute product in base 10
:P*Q→R
:1+mod(R,26)→REM
:0→I
// Convert base 10 product to base 26
:While (R≠0)
:I+1→I
// Display the product in base 26 using alphabetic characters.
// REM is the remainder when R is divided by 26 and gives the position in the alphabet
:Outpt(8,21-I,sub(A,REM,1))
:iPart (R/26)→R
:1+mod(R,26)→REM
:End