It's kinda hard to optimize anything without knowing what it does.

For example, knowing what range of inputs a value can have allows you to optimize things to a smaller size. Otherwise, we would have to ensure that it works the same way even if given an invalid argument.

I'll take a swing at optimizing this when I get to my computer, but it would be helpful to know what this code is doing.

EDIT: am at computer; is optimization time.

**Quote:**

The reason I'm posting this is so I may learn.

To help with that, I'm going to try to explain what I'm doing while optimizing:

You seem to be missing a few End statements, I went ahead and added those on to the end.

Also, you seem to be using x instead of E on some of your Disp statements, like you copied it directly from iPhoenix without running it first. Does the code you currently have work the way you want it to in all cases? If not, you might want to check it before giving it to us to optimize.

First thing I see is that this bit is repeated:

**Code:** ```
E+D->F
```

D->E

F->D

As per the Don't Repeat Yourself principle, we can probably change the code so that this bit is only in there once. To do that, let's just change the "switch" bit so that it stores the number of times the For loop will run and the digit we want to extract in variables (L and P, respectively). Then, at the end of the code, we can put that For loop in there once, and also include a more general digit finder.

**Code:** ```
Prompt B
```

Disp "Digits Behind"

Input "Decimal=",A

If remainder(A,B)=0

Then

A/B->L

0->P

Else

//Code below here is when A/B has a remainder

If B=2

Then

(A+1)/B->L

1->P

Else

If B=3 and Remainder(A,B)=1

Then

(A+2)/B->L

2->P

Else

If B=3 and Remainder(A,B)=2

Then

(A+1)/B->L

1->P

End

End

End

End

End

1->D

1->E

For(C,2,L

E+D->F

D->E

F->D

End

Disp remainder(int(E/10^P),10

You can further move the /B which is common to each case into the code which is only run once. Also, not(X is faster than X=0. I also removed an Else clause with nothing in it.

**Code:** ```
Prompt B
```

Disp "Digits Behind"

Input "Decimal=",A

If not(remainder(A,B

Then

A->L

0->P

Else

//Code below here is when A/B has a remainder

If B=2

Then

A+1->L

1->P

Else

If B=3 and Remainder(A,B)=1

Then

A+2->L

2->P

Else

If B=3 and Remainder(A,B)=2

Then

A+1->L

1->P

End

End

End

End

1->D

1->E

For(C,2,L/B

E+D->F

D->E

F->D

End

Disp remainder(int(E/10^P),10

If there is any kind of relationship at all between A and B (which there seems to be, considering the way the conditions are structured), this code could probably be shrunk significantly and generalized to work with a wider range of values (eg. more than 3 digits) if you told us what it was. It looks like you are finding a certain number in the Fibonacci sequence.

Based on the cases given, it looks like the value of the number added to A is B-remainder(A,B), which is also P. If this is true, then you could remove all your conditional logic (the If statements) and just calculate P that way, like this:

**Code:** ```
B-remainder(A,B)->P
```

A+P->L

...

For(C,2,L/B

...

Now, the value of L is only being computed once, so we don't really need to store it as a variable any more. We can move the calculations into the for loop itself.

**Code:** ```
Prompt B
```

Disp "Digits Behind"

Input "Decimal=",A

B-remainder(A,B->P

1->D

1->E

For(C,2,(A+P)/B

E+D->F

D->E

F->D

End

Disp remainder(int(E/10^P),10

Now this probably looks really tiny already, but we can probably get it smaller by using calculus or something (I don't really understand this bit well, this is all from Google) (EDIT: Weregoose says it's Binet's formula and Google agrees with him). This is outside the scope of optimization (I think?), but there is a faster way to get the nth number of the Fibonacci sequence (credit to Binet (whoever he was) for the formula and to Xeda of tibasicdev for making it nice and easy to copy):

**Code:** ```
.5(1+√(5
```

round(Ans^N/√(5),0

Putting that in our code makes

**Code:** ```
Prompt B
```

Disp "Digits Behind"

Input "Decimal=",A

B-remainder(A,B->P

2cos(.2π

round(Ans^((A+P)/B)/√(5),0

Disp remainder(int(Ans/10^P),10

We can further remove the redundant rounding and move the math bit all onto one line.

**Code:** ```
Prompt B
```

Disp "Digits Behind"

Input "Decimal=",A

B-remainder(A,B->P

2cos(.2π

Disp remainder(round(Ans^((A+P)/B)/√(5)/10^P,0),10

You could probably optimize this further, but I've been typing this for like two hours and I think I'm done for today. You also might want to check that this works, as I may have forgotten a parenthesis somewhere. Hopefully this has been somewhat informative.