Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
The code is posted below. Don't worry about what it does. I just need it to be shorter.

Code:

Prompt B
Disp "Digits Behind"
Input "Decimal=",A
1->D
1->E
If remainder(A,B)=0
Then
For(C,2,(A/B))
E+D->F
D->E
F->D
End
Disp remainder(E,10)
Else
//Code below here is when A/B has a remainder
If B=2
Then
For(C,2,((A+1)/B)
E+D->f
D->E
F->D
End
Disp int(E/10)
Else
If B=3 and Remainder(A,B)=1
Then
For(C,2,((A+2)/B
E+D->f
D->E
F->D
End
Disp int (x/100)
Else
If B=3 and Remainder(A,B)=2
Then
For(C,2,((A+1)/B
E+D->f
D->E
F->D
End
Disp int (x/10)
Else
End


Thanks to anyone who helps. The reason I'm posting this is so I may learn. So please criticize it. (It isn't good)
Get rid of ending parentheses and quotes at the end of a line or before a →.

Also sub( can be used as a cheat to divide by 100. Still don't know why TI did that. I am on mobile, so not dissecting the whole code, by here are basic optimizations:


Code:

Prompt B
Disp "Digits Behind
Input "Decimal=",A
1->D
1->E
If not(remainder(A,B
Then
For(C,2,A/B
E+D->F
D->E
F->D
End
Disp remainder(E,10
Else
//Code below here is when A/B has a remainder
If B=2
Then
For(C,2,(A+1)/B
E+D->F
D->E
F->D
End
Disp int(E.1
Else
If B=3 and 1=Remainder(A,B
Then
For(C,2,(A+2)/B
E+D->F
D->E
F->D
End
Disp int(sub(E
Else
If B=3 and 2=remainder(A,B
Then
For(C,2,(A+1)/B
E+D->F
D->E
F->D
End
Disp int (x.1
End
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.
I have no idea what this code does, but you don't need to put Disp for the last line. If the last non-empty line is value returning, the value will be displayed. Also, you should probably add a Radian or Decimal at the beginning of the program since you're using trig.

EDIT: The only part that uses trig is the golden ratio, so even though it's not quite as short, if you consider that you don't need to specify Radians or Degrees, it is shorter to replace this:

Code:
2cos(.2π

with this:

Code:
.5(1+√5

And it has the added bonus of not messing with the user's angle mode.

Code:
int(round(√(.8)cosh(Bsinh‾(.5


is the shortest way to get the Bth Fibonacci number, originally due to Weregoose. No trig necessary. I mentioned this here: https://codegolf.stackexchange.com/a/52817/39328
commandblockguy wrote:

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.


YOU ARE THE GOAT (Greatest Of All Time)
I can't thank you enough for the effort you put forth in making this forum post.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement