Why is TI-Basic so slow?

I decided to implement a program that can find the GCD of any two numbers (including non-integers) in TI-Basic. I used it just in Java, so I know that it works. It works great in TI-Basic, but compared to the built-in function, gcd(it is very sluggish; the function gcd(seems to get the result in milliseconds, where mine can take a few seconds. Why is TI-Basic so slower than the predefined functions of the calculator?

The code


Here is the program code in TI-Basic, for verification:

PROGRAM:GCD

:ClrHome
:Disp "Greatest Common","    Divisor","      ---"
:Input "First number? ",X
:Input "Second number? ",Y
:
:X→I
:Y→J
:
:If (I≠int(I) or J≠int(J))
:Then
:ClrHome
:Disp "Non-integer","inputs may be","innacurate!",""
:End
:If (I=1 or J=1)
:Then
:1→I
:1→J
:Goto Z
:End
:For(C,0,2^8)
:If I=J
:Goto Z
:
:If I>J
:I-J→I
:
:If J>I
:J-I→J
:
:End
:
:Disp "This is a hard","one! Thinking","harder..."
:
:For(C,0,2^15)
:If (I=J)
:Goto Z
:While (I>J)
:I-J→I
:C+1→C
:End
:While (J>I)
:J-I→J
:C+1→C
:End
:End
:
:Disp "TIMED OUT!","Either:",J,"or"
:Pause
:
:Lbl Z
:ClrHome
:Disp "GCD of",X,"and",Y,"is",I

Disclaimer: . This is the result of me looking at my TI-84 and typing it here. Some typos may be in it, although I tried my best to keep it the same

, , , :

program gcd()
{
Console.clear();
Console.writeln("Greatest Common");
Console.writeln("    Divisor");
Console.writeln("      ---");

float X = Console.readFloat("First Number? ");
float Y = Console.readFloat("Second number? ");

float I = X;
float J = Y;

if (I != (int)I || J != (int)J)
{
  Console.clear();
  Console.writeln("Non-integer");
  Console.writeln("inputs may be");
  Console.writeln("inaccurate!");
  Console.writeln("");
}
if (I == 1 or J == 1)
{
  I = 1;
  J = 1;
  goto Z;
}

for(int C = 0, limit = Math.pow(2,8); C < limit; C++)
{
  if (I == J)
    goto Z;

  if (I > J)
    I = I - J;

  if (J > I)
    J = J - I;
}

Console.writeln("This is a hard");
Console.writeln("one! Thinking");
Console.writeln("harder...");

for(int C = 0, limit = Math.pow(2,15); C < limit; C++)
{
  if (I == J)
    goto z;
  while (I > J)
  {
    I = I - J;
    C++;
  }
  while (J>I)
  {
    J = J-I;
    C++;
  }
}

Console.writeln("TIMED OUT!");
Console.writeln("Either:");
Console.writeln(J);
Console.writeln("or");
Console.pause();

Z:
Console.clear();
Console.writeln("GCD of");
Console.writeln(X);
Console.writeln("and");
Console.writeln(Y);
Console.writeln("is");
Console.writeln(I);
}
+6
3

, - .

, .

+8

, , , - clrHome, , clr-home , , , , , Ti-83 plus, disp input , , prompt fist number','second number'

+1

You can fix most of the code, for example, without using Goto, leaving brackets at the ends of lines, without using ClrHome, etc. Additional information: https://en.wikibooks.org/wiki/TI-Basic_Z80_Programming/Tips,_Tricks_and_Optimizations

0
source

All Articles