Code:
Lbl 1
ClrHome
Disp "ENTER NUMBER
Input "NUMBER? ",A
startTmr→H
seq(N,N,1,A→X
A/⌊X→Y
fPart(⌊Y)=0→Z
⌊Z*⌊X→X
⌊Z*⌊Y→Y
SortA(⌊X,⌊Y
dim(⌊X)/2
If ⌊X(iPart(dim(⌊X)/2))=0:Then
SortD(⌊X,⌊Y
While ⌊X(iPart(dim(⌊X)/2))=0
iPart(dim(⌊X)/2→dim(⌊X
iPart(dim(⌊Y)/2→dim(⌊Y
End
Goto 3
Else
Lbl 3
SortA(⌊X,⌊Y
While min(⌊X)=0
List(cumSum(⌊X→X
List(cumSum(⌊Y→Y
End
End
iPart((dim(⌊X)/2)+.5→M
M→dim(⌊X
M→dim(⌊Y
SortA(⌊X,⌊Y
Listmatr(⌊X,(⌊Y),[J]
Disp [J]
Output(5,1,Str0
Output(5,6,checkTmr(H
Pause
Menu("DO MORE?","YES",1,"NO",2
Lbl 2
ClrHome
Disp "GOODBYE!


If anyone has any optimizations for this I would love to see them! The parts I want to focus on are how to scroll if the matrix is too long, and how to bypass the 999 number limit on lists.

Thanks in advance!
Hi, welcome to Cemetech!

Here are few optimizations to your current approach:
  • The dim(|LX)/2 line has no effect and can be removed.
  • X=0 is equivalent to not(X, which is smaller and slightly faster and often allows you to omit an additional parenthesis.
  • If X:Then:A:Goto 3:Else:Lbl 3:B:End is equivalent to If X:Then:A:End:B.
  • The |L1, |L2... vars are smaller and faster than named variables like |LX.
  • Storing to dim(X updates Ans, so you don't need to use a temporary variable when updating the dimensions of |LX and |LY at the same time.
  • You aren't setting Str0 anywhere, but still using it, so this doesn't run on a freshly reset calculator.
  • seq(N,N,1,A->X can be replaced with cumSum(binomcdf(A-1,0->X.
  • It looks like the contents of the If statement are for deleting zeroes from the list, with the If part being used to make sure that you only ever do a number of pops in the Else statement that's less than the number of remaining elements in the list - clever!
    Because For( loops are slow, you might be able to improve it even further by counting the number of zeroes in the list (using sum(not(|LX))), then subtracting that exact amount from dim(|LX after sorting it.
  • You're currently calculating the full list of factors, then cutting it in half to eliminate duplicates. You can save a lot of work by reducing the initial size of |LX instead. The duplicate factors that we want to remove have x > y. We can plug in y = A/x to get x > A/x and then simplify to get x > √A. So, instead of putting all x from 1 to A in |LX, we can limit it to just 1 to iPart(sqrt(A)) and remove the part where you cut the result list in half. This also helps us a lot with regards to the 999-element limit for lists, since the size of the list grows a lot slower - even A=999999 would fit in a 999-element list.


As for the matrix scrolling - you can't scroll a matrix that was displayed with Disp, but Pause [J] will also display [J], and allows scrolling with the arrow keys.

EDIT: |L1 is not in fact smaller than |LX, oops
Thank you so much! I do have some questions though if you do not mind to much.

1. when you say X=0 is equivalent to not(X), what part of the code are you referring too?
2. Thank you for the heads up about Str0, I have updated the code to set it as "TIME:".
3. How would cumsum(binomcdf(A-1,0 ->X work? I am new to coding in general and specifically using TI-BASIC and I like to have a general understanding of what code does before using it.
4. Thank you for the complement about deleting zeroes! Same question as number 3 about sum(not(|LX)) , that is to say, how exactly does it count the zeroes?
4. For your last point about reducing the initial list size, when you use X and Y are they just numbers in the list? So when you say x>y that means that we are removing any number from LX where number x>any number y in LY?
5. If I am using cumsum(binomcdf( in conjunction with the ipart(sqrt(A would I say cumsum(binomcdf(1, ipart(sqrt(A -> X, or do I put it in a different way.

Thank you so much for the tips! It will be a lot of help.
Snewo12 wrote:
when you say X=0 is equivalent to not(X), what part of the code are you referring too?


You can use that anywhere that you have an =0 in the code - so, when assigning to Z, when checking if a list item is zero, etc.

Snewo12 wrote:
How would cumsum(binomcdf(A-1,0 ->X work? I am new to coding in general and specifically using TI-BASIC and I like to have a general understanding of what code does before using it.


The cumsum(binomcdf(A-1,0 trick is a well-known optimization for generating lists - the TI-BASIC Developer page for binomcdf( has more information, but the gist is that binomcdf(X,0 always produces a list with X+1 ones in it. Adding a cumSum( to the front will thus give you a list {1, 2, 3..., X+1}.

Snewo12 wrote:
Thank you for the complement about deleting zeroes! Same question about sum(not(|LX)) as number 3, that is to say, how exactly does it count the zeroes?


not( returns 1 if the input is 0 and 0 otherwise. This gets vectorized in lists, so not({0,1,0})={1,0,1}. The sum( of a list with only 0s and 1s in it is just the number of 1s, which is the number of 0s in the original list.

Snewo12 wrote:
For your last point about reducing the initial list size, when you use X and Y are they just numbers in the list? So when you say x>y that means that we are removing any number from LX where number x>any number y in LY?

Yeah, that was a bit of shorthand on my part that I probably could have explained better - lowercase x is just an item from |LX, with lowercase y being the corresponding factor in |LY. My point was that you'll never print out a pair of factors (x, y) where the first factor (x) is larger than the second factor (y), because at that point you would have already printed out (y, x).

Snewo12 wrote:
If I am using cumsum(binomcdf( in conjunction with the ipart(sqrt(A would I say cumsum(binomcdf(1, ipart(sqrt(A -> X, or do I put it in a different way.

The argument to binomcdf( that determines the list length is the first one, not the second - you'll want to do cumSum(binomcdf(iPart(sqrt(A))-1,0->X
Some first notes:
- Double-check your usage of Str0. You read from but never store to this variable.
- Implied multiplication works and saves a byte every time.
- You have to be careful with how you present your results. Presenting it as a matrix looks pretty, but the program will crash (at least on the CSE and later?) if more than 20 factors are found. This does not occur for numbers under 999, but 840 gets dangerously close Wink
- Multiplying by half instead of dividing by two can save you closing parentheses- multiplication is commutative (addition is too). Eg. iPart((dim(|LX)/2)+.5->M becomes iPart(.5+.5dim(|LX->M (also notice that I dropped the unnecessary parenthesis- trust the order of operations)
- I second commandz' suggestion of only searching up to sqrt(n).

Good work!
Hello! Thank you for the suggestions. I understand the issue with matrices, but have no other ideas on how to display the info. Do you have any suggestions?

EDIT: I have tried the number 999999, which has 32 factor pairs, with the new and improved program and the matrix didn't crash anything. I do not know if this is an anomaly, or if it is normal.

So far, this is what I have.


Code:
Lbl 1
"TIME:"→Str0
ClrHome
Disp "ENTER NUMBER
Input "NUMBER? ",A
startTmr→H
cumSum(binomcdf(iPart(√(A)-1),0→X
A/⌊X→Y
not(fPart(⌊Y→Z
⌊Z*⌊X→X
⌊Z*⌊Y→Y
SortD(⌊X,⌊Y
dim(⌊X)-sum(not(⌊X→dim(⌊X
Ans→dim(⌊Y
SortA(⌊X,⌊Y
Listmatr(⌊X,(⌊Y),[J]
Output(5,1,Str0
Output(5,6,checkTmr(H
Pause [J]
Menu("DO MORE?","YES",1,"NO",2
Lbl 2
ClrHome
Disp "GOODBYE!


How does it look?

(admin edit: merged a double-post)
iPhoenix wrote:
Presenting it as a matrix looks pretty, but the program will crash (at least on the CSE and later?) if more than 20 factors are found.

Snewo12 wrote:
I have tried the number 999999, which has 32 factor pairs, with the new and improved program and the matrix didn't crash anything. I do not know if this is an anomaly, or if it is normal.

I believe that the limitation is:
  • No dimension of a matrix can be larger than 99
  • On the CSE and CE, the total number of elements in a matrix (i.e. the product of its dimensions) can be no larger than 400


You don't need to store a string in a variable to display it - you can use Output(5,1,"TIME:. The parentheses on List>matr( are also unnecessary.

An optimization I just noticed now - if the non-zero elements of |LX are in ascending order, the non-zero elements of |LY will be in descending order, and vice versa. So, if you replaced the SortD(|LX,|LY with SortD(|LY,|LX, the zero elements will still end up at the end, but the non-zero elements will be in the correct order, saving you from having to do the SortA(|LX,|LY to reverse the lists later.

It may be worth trying to get rid of one of the lists altogether, since you can always get |LX from |LY or vice versa by dividing by A. I don't know off the top of my head whether adding the second list to the SortA( would be slower than doing the division again, but probably worth testing. Contrary to the help description for List>matr(, you don't actually need to pass in list names as arguments - anything that returns a list is fine, so List>matr(A/|LY,|LY,[J] would also work.
Thank you! I have noticed a slight limitation and have added an if statement to remedy it. If X<4, then the system will error because, for instance, the ipart(sqrt(3 will equal 1, and since we are subtracting 1 from the list maker (cumsum(binomcdf(ipart(sqrt(3)-1,0 -> X)))) the list maker errors because it cannot make a list from 0 to 0. Since I want it to be able to do any number from 1-999999, I do need the IF statement. Smile



Code:
Lbl 1
ClrHome
Disp "ENTER NUMBER
Input "NUMBER? ",A
startTmr→H
If A>3:Then
cumSum(binomcdf(iPart(√(A)-1),0→X
Else
{1}→⌊X
End
A/⌊X→Y
not(fPart(⌊Y→Z
⌊Z*⌊X→X
⌊Z*⌊Y→Y
SortD(⌊Y,⌊X
dim(⌊X)-sum(not(⌊X→dim(⌊X
Ans→dim(⌊Y
Listmatr(⌊X,(⌊Y),[J]
Output(5,1,"TIME:"
Output(5,6,checkTmr(H
Pause [J]
Menu("DO MORE?","YES",1,"NO",2
Lbl 2
ClrHome
Disp "GOODBYE!


Also I could not figure out how to use the A/LY in the list>matrix because I need A/LX -> Y to find the non-whole numbers and eliminate them
instead of
Code:
If A>3:Then
cumSum(binomcdf(iPart(√(A)-1),0→X
Else
{1}→⌊X
End

you could do

Code:
{1
If A>3
cumSum(binomcdf(iPart(√(A)-1),0
Ans→X
  
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 UTC - 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