2
\$\begingroup\$

I am attempting to create a program that can, given an integer as input, simplify the radicand by removing all square factors. For use on my TI 84+ CE Code: for clarity reasons I've indented conditional blocks and added a new line after Delvar.

If not(A:Input “√(“,A
1→Y
While not(remainder(A,4
    A/4→A
    2Y→Y
End
3→X
While X²<=A
    X²→θ
    While not(remainder(A,θ
        A/θ→A
        XY→Y
    End
    X+2→X
End
{Y,A
DelVar A
If 1=Ans(2:Then
    Disp “√(“+toString(Ans(2))+”)
Else:If 1=Ans(1:Then
    Disp Ans(1
Else
    Disp toString(Ans(1))+“√(“+toString(Ans(2))+”)

The program asks for input if one is not already provided in the A variable. It also initializes a Y variable to store the removed factors (part outside the radical sign). It then proceeds, for X from 2 counting up until X is greater than the square root of A to successively divide out squares of X and multiply Y by X for each iteration. After doing this for 2, it only tests odd numbers. At the end, A stores the value left inside the radicand. If A=1, then it simply displays Y as the answer. If Y=1, then it displays √Y as the answer. Otherwise, it shows A√Y as the result.

The worst case input for the algorithm is any number without a square factor, because it then has to run all the iterations to complete.

Are there any optimizations I can make? As space is not a problem (yet) on my calculator, I would prefer a speed optimization over a space optimization, but both are welcome.

\$\endgroup\$
  • 1
    \$\begingroup\$ It would be helpful if you gave a brief algorithm overview. given the formatting, and the fact that it is a kind of obscure language, it will let people give better advice. \$\endgroup\$ – Oscar Smith Jan 11 '18 at 6:47
  • \$\begingroup\$ Also indentation and adding closing brackets would help - at least here. The 84s display is probably not suited for the latter one. \$\endgroup\$ – Markus Deibel Jan 11 '18 at 8:00
  • 1
    \$\begingroup\$ If you had a list of prime numbers on my calculator (at some point I generated the first 999, which is the most that can fit in a list), you could iterate those instead of all the numbers (then iterate all the odds after the primes run out). \$\endgroup\$ – Solomon Ucko Feb 28 '18 at 20:10
  • \$\begingroup\$ How large is the integer? It seems like you need an integer factorisation algorithm, depending on the size of the number, different algorithms would be appropriate. \$\endgroup\$ – George Barwood Jan 23 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.