This is Pascal's Triangle:

The top starts with row 0. I created a program that can calculate the numbers of a row and store them in L₂. The program can also use pascal's triangle for the binomial theorem. Which is (X+A)^B, with B being pascal's triangle's row number. One problem. In Basic is there any command that either can:
1. Display both numbers and strings at the same time?
Ex: Display: "+" A^Q "X^" B
or
2. Tell the length (in digits) of a variable or variables?
Ex: length of N, or length of N*A
1) I'm assuming you are trying to display the string as if it were a single line, in which case you will need to store the numbers to strings and then work with a single string. Currently, without libs, the simplest way to do this is with a routine that utilises the Equ►String( command also known as ti-basic's perpetual thornRazz

Code:
:{0,1→L₁
:{0,N→L₂
:LinReg(ax+b) Y₁
:Equ►String(Y₁,Str1
:sub(Str1,1,length(Str1)-3→Str1

This will take expr(N) and store it to Str1.
2) If you are only dealing with integers (which is probably the case since you are talking about pascal's triangle), then

Code:
1+int(log(N

Is the length in digits of the variable N, for example, if N=854534, then 1+int(log(N))=6
mr womp womp wrote:
1) I'm assuming you are trying to display the string as if it were a single line, in which case you will need to store the numbers to strings and then work with a single string. Currently, without libs, the simplest way to do this is with a routine that utilises the Equ►String( command also known as ti-basic's perpetual thornRazz

Code:
:{0,1→L₁
:{0,N→L₂
:LinReg(ax+b) Y₁
:Equ►String(Y₁,Str1
:sub(Str1,1,length(Str1)-3→Str1

This will take expr(N) and store it to Str1.
2) If you are only dealing with integers (which is probably the case since you are talking about pascal's triangle), then

Code:
1+int(log(N

Is the length in digits of the variable N, for example, if N=854534, then 1+int(log(N))=6

Thank you. I've seen both examples, just never knew what they were for. So if I wanted to find the length of a floating point number, then I'd have to use fPart and iPart I'm assuming and add the two answers together.
Adding the fPart and iPart of a number together will result in the original number... At that point, if you need to figure out the length of a number with decimals, I'm sure you could come up with a pretty messy routine that could do it, but I'd suggest just storing the number to a string with the first routine and using the length( command.
But, then you'd have a decimal so you'd have to subtract by 1. But yes thank you for giving me the faster version.
Interesting that you made a program to calculate the numbers in a row. Without input one can do it in 10 bytes Smile
seq(N nCr X, X,0, N
I am using that to output the row. I'm making the program for those who don't want to take the time to memorize that sequence. Plus, this program involves the binomial theorem also.
I keep having a syntax error here:

Code:
{0,1}→L₁
{0,B}→L₂                       //power, triangle row
{0,J}→L₃                       //Current element of triangle row
{0,H}→L₅                      //Last element of binomial
LinReg(ax+b) L₁,L₂,Y₁
LinReg(ax+b) L₁,L₃,Y₂
LinReg(ax+b) L₁,L₅,Y₄          //error on this line
Equ►String(Y₁,Str1
Equ►String(Y₂,Str2
Equ►String(Y₄,Str4
sub(Str1,1,length(Str1)-3→Str1
sub(Str2,1,length(Str2)-3→Str2
sub(Str4,1,length(Str4)-3→Str4
DelVar Y₁DelVar Y₂DelVar Y₄
seanlego23 wrote:
I keep having a syntax error here:

Code:
{0,1}→L₁
{0,B}→L₂                       //power, triangle row
{0,J}→L₃                       //Current element of triangle row
{0,H}→L₅                      //Last element of binomial
LinReg(ax+b) L₁,L₂,Y₁
LinReg(ax+b) L₁,L₃,Y₂
LinReg(ax+b) L₁,L₅,Y₄          //error on this line
Equ►String(Y₁,Str1
Equ►String(Y₂,Str2
Equ►String(Y₄,Str4
sub(Str1,1,length(Str1)-3→Str1
sub(Str2,1,length(Str2)-3→Str2
sub(Str4,1,length(Str4)-3→Str4
DelVar Y₁DelVar Y₂DelVar Y₄

Since you are doing it three times, I don't know if it would be better, but it would definitely be more elegant, to do all three at once using the QuadReg command like this:

Code:
{0,1,2→L₁
seq(Bχ+Jχ+H,χ,0,2→L₂
QuadReg Y₁
Equ►String(Y₁,Str1

In Str1 will be a quadratic equation with three arguemnts, each one being the numbers contained in the variables B.J and H respectively.
mr womp womp wrote:
seanlego23 wrote:
I keep having a syntax error here:

Code:
{0,1}→L₁
{0,B}→L₂                       //power, triangle row
{0,J}→L₃                       //Current element of triangle row
{0,H}→L₅                      //Last element of binomial
LinReg(ax+b) L₁,L₂,Y₁
LinReg(ax+b) L₁,L₃,Y₂
LinReg(ax+b) L₁,L₅,Y₄          //error on this line
Equ►String(Y₁,Str1
Equ►String(Y₂,Str2
Equ►String(Y₄,Str4
sub(Str1,1,length(Str1)-3→Str1
sub(Str2,1,length(Str2)-3→Str2
sub(Str4,1,length(Str4)-3→Str4
DelVar Y₁DelVar Y₂DelVar Y₄

Since you are doing it three times, I don't know if it would be better, but it would definitely be more elegant, to do all three at once using the QuadReg command like this:

Code:
{0,1,2→L₁
seq(Bχ+Jχ+H,χ,0,2→L₂
QuadReg
Equ►String(Y₁,Str1

In Str1 will be a quadratic equation with three arguemnts, each one being the numbers contained in the variables B.J and H respectively.

Then I can just use the sub() command to split them up. Thanks.
Yep, also a quick tip when using the regression commands, the default settings are already
Code:
L₁,L₂

So if you are using those lists, then you don't need to put them, kind of like how an increment of 1 is assumed if you skip the final argument in a For() loop
mr womp womp wrote:
Yep, also a quick tip when using the regression commands, the default settings are already
Code:
L₁,L₂

So if you are using those lists, then you don't need to put them, kind of like how an increment of 1 is assumed if you skip the final argument in a For() loop

That's true. Thanks for reminding me. I primarily use C now so I've forgotten a bit.
The QuadReg Equation comes up with B,J, and H, but B and J are just barely off. If B=2 then it shows 1.9999... if J = -1 then it shows -0.9999...
How do I fix this? Also, If B, J, and H, are different digit lengths per the user input, how do I figure out how long it is in the code?
seanlego23 wrote:
The QuadReg Equation comes up with B,J, and H, but B and J are just barely off. If B=2 then it shows 1.9999... if J = -1 then it shows -0.9999...
How do I fix this? Also, If B, J, and H, are different digit lengths per the user input, how do I figure out how long it is in the code?

AHHH I was afrait it would do that, once it's in a string, it's pretty much too late to mess with it once it's in a string, luckily, there is one simple thing you can do that will fix it which is running the Fix command before the whole operation so that when it does the QuadReg, it immediately round that off, you could probably get away with Fix 7, so it would look like this:

Code:
Fix 7
{0,1,2→L₁
seq(Bχ+Jχ+H,χ,0,2→L₂
QuadReg
Equ►String(Y₁,Str1

Oh and if you're still geting rounding errors, you can always reduce the accuracy down to 6. but I doubt that will be necessary
Thanks that worked. How would you get rid of things in the middle/beginning of a string?
Here's my code:

Code:
Lbl 0
ClrHome
Menu("OPTIONS","ROW#",A,"BIONOMIAL THEOREM",B,"QUIT",C
Lbl A
Output(10,1,"ROW#≤69
Input "ROW:",C
If C≥70:Then
Output(10,15,"ERROR
Goto A
End
seq(C nCr X,X,0,C)→L₆
Disp L₆
Pause 
Goto 0
Lbl B
ClrHome
0→D:1→K:1→Z:1→Y
"+→Str0
"X^→Str9
Output(4,1,"D(X+A)^B
Output(5,1,"B≤69
Input "A=",A
Input "B=",B
Input "D=",D
If B≥70:Then
Output(10,1,"ERROR
Goto B
End
If D=0:Then
Output(4,1,"EQUALS ZERO
Pause
Goto B
End
If D≠1:Goto 1
ClrHome
For(Q,0,B
(B!)/(Q!((B-Q)!))→N
Q-K→J
N(A^Q)→G
A^B→H
Fix 6
{0,1,2}→L₁
seq(BX+JX+H,X,0,2,1→L₂
seq(JX+HX+B,X,0,2,1→L₃
seq(HX+BX+J,X,0,2,1→L₄
QuadReg Y₁
QuadReg L₁,L₃,Y₂
QuadReg L₁,L₄,Y₃
{0,1}→L₁
{0,J}→L₂
{0,H}→L₃
{0,B}→L₄
LinReg(ax+b) Y₄
LinReg(ax+b) L₁,L₃,Y₅
LinReg(ax+b) L₁,L₄,Y₆
EquString(Y₁,Str1
EquString(Y₂,Str2
EquString(Y₃,Str3
EquString(Y₄,Str4
EquString(Y₅,Str5
EquString(Y₆,Str6
sub(Str4,1,length(Str4)-3→Str4
sub(Str5,1,length(Str5)-3→Str5
sub(Str6,1,length(Str6)-3→Str6
sub(Str1,1,length(Str1)-(length(Str4)+length(Str5)+6)→Str1
sub(Str2,1,length(Str2)-(length(Str5)+length(Str6)+6)→Str2
sub(Str3,1,length(Str3)-(length(Str6)+length(Str4)+6)→Str3
If G≠1:Then
{0,G}→L₂
LinReg(ax+b) L₁,L₂,Y₁
EquString(Y₁,Str7
sub(Str7,1,length(Str7)-3→Str7
End
If B<20:Then
If not(Q):Then
Output(1,1,Str9+Str1
Y+length(Str9+Str1→Y
End
If Q=B:Output(Z,Y,Str0+Str3
If Q and Q≠B:Then
If (A^Q):Then
Output(Z,Y,Str0+Str7+Str9+Str2
K+1→K:Y+length(Str0+Str7+Str9+Str2→Y
End
If not(A^Q):Goto E
End:End
If B≥20:Then
If not(Q):Then
Output(1,1,Str9+Str1
Y+length(Str9+Str1→Y
End
If Q=B:Output(Z,Y,Str0+Str3
If Q and Q≠B:Then
If (A^Q):Then
Output(Z,Y,Str0+Str7+Str9+Str2
K+1→K:Y+length(Str0+Str7+Str9+Str2→Y
End
If not(A^Q):Goto E
End
If fPart(Z/10)=9:Then
Pause
10:Asm(prgmSHFTUP
1→Z
End
If Y>26:Then
(Y-26)+1→Y:Z+1→Z
End:End
Lbl E
Output(Z+1,1,"DONE
Pause
Float
Goto 0
Lbl 1
ClrHome
For(Q,0,B
(B!)/(Q!((B-Q)!))→N
Q-K→J
DN(A^Q)→G
D(A^B)→H
Fix 6
{0,1,2}→L₁
seq(BX+JX+H,X,0,2,1→L₂
seq(JX+HX+B,X,0,2,1→L₃
seq(HX+BX+J,X,0,2,1→L₄
QuadReg Y₁
QuadReg L₁,L₃,Y₂
QuadReg L₁,L₄,Y₃
{0,1}→L₁
{0,J}→L₂
{0,H}→L₃
{0,B}→L₄
{0,D}→L₅
LinReg(ax+b) Y₄
LinReg(ax+b) L₁,L₃,Y₅
LinReg(ax+b) L₁,L₄,Y₆
LinReg(ax+b) L₁,L₅,Y₇
EquString(Y₁,Str1
EquString(Y₂,Str2
EquString(Y₃,Str3
EquString(Y₄,Str4
EquString(Y₅,Str5
EquString(Y₆,Str6
EquString(Y₇,Str7
sub(Str4,1,length(Str4)-3→Str4
sub(Str5,1,length(Str5)-3→Str5
sub(Str6,1,length(Str6)-3→Str6
sub(Str1,1,length(Str1)-(length(Str4)+length(Str5)+5)→Str1
sub(Str2,1,length(Str2)-(length(Str5)+length(Str6)+5)→Str2
sub(Str3,1,length(Str3)-(length(Str6)+length(Str4)+5)→Str3
If G≠1:Then
{0,G}→L₂
LinReg(ax+b) L₁,L₂,Y₁
EquString(Y₁,Str8
sub(Str8,1,length(Str8)-3→Str8
End
If B<20:Then
If not(Q):Then
Output(1,1,Str7+Str9+Str1
Y+length(Str9+Str1→Y
End
If Q=B:Output(Z,Y,Str0+Str3
If Q and Q≠B:Then
If (A^Q):Then
Output(Z,Y,Str0+Str8+Str9+Str2
K+1→K:Y+length(Str0+Str8+Str9+Str2→Y
End
If not(A^Q):Goto F
End:End
If B≥20:Then
If not(Q):Then
Output(1,1,Str7+Str9+Str1
Y+length(Str9+Str1→Y
End
If Q=B:Output(Z,Y,Str0+Str3
If Q and Q≠B:Then
If (A^Q):Then
Output(Z,Y,Str0+Str8+Str9+Str2
K+1→K:Y+length(Str0+Str8+Str9+Str2→Y
End
If not(A^Q):Goto F
End
If fPart(Z/10)=9:Then
Pause
10:Asm(prgmSHFTUP
1→Z
End
If Y>26:Then
(Y-26)+1→Y:Z+1→Z
End:End
Lbl F
Output(Z+1,1,"DONE
Pause
Float
Goto 0
Lbl C
Float

It's error free as far as I know. However, if I run it and click Binomial Theorem, with A=5,B=2,D=1, it should come out with X^2+10X+25 right? Nope, it just prints X^2. And Q=0 at the end. I don't understand why.
Help me find my error. Any optimization ideas are welcome because this is a lot of code. Thanks. Smile
I haven't taken the time to look through the whole thing, but some things I noticed...:

Code:
Fix 6
{0,1,2}→L₁
seq(BX+JX+H,X,0,2,1→L₂
seq(JX+HX+B,X,0,2,1→L₃
seq(HX+BX+J,X,0,2,1→L₄
QuadReg Y₁
QuadReg L₁,L₃,Y₂
QuadReg L₁,L₄,Y₃
{0,1}→L₁
{0,J}→L₂
{0,H}→L₃
{0,B}→L₄
LinReg(ax+b) Y₄
LinReg(ax+b) L₁,L₃,Y₅
LinReg(ax+b) L₁,L₄,Y₆

This stuff is missing the point of using QuadReg completely.... If you're doing it this many times, it might be worth considering a loop with a linreg in it, or if you want to try real hard, go with QuartReg and get a quartic function to do 5 at at a time but these "all at once" things don't seem to be of your liking Razz

Code:
If fPart(Z/10)=9

By definition, the fPart of a number will never be bigger than 0.99999999... so this conditional will never go through.

Code:
seq(C nCr X,X,0,C)→L₆
Disp L₆

Here you don't reuse L₆, so you could just do

Code:
Disp seq(C nCr X,X,0,C

Here, you could use the Prompt command...

Code:
Input "A=",A
Input "B=",B
Input "D=",D

could be

Code:
Prompt A,B,D

The only difference would be that it would say "A=?" instead of "A="

Code:
(Y-26)+1→Y
could be
Y-25→Y
this doesn't make much of a difference but I try to keep things simplified...
mr womp womp wrote:
I haven't taken the time to look through the whole thing, but some things I noticed...:

Code:
Fix 6
{0,1,2}→L₁
seq(BX+JX+H,X,0,2,1→L₂
seq(JX+HX+B,X,0,2,1→L₃
seq(HX+BX+J,X,0,2,1→L₄
QuadReg Y₁
QuadReg L₁,L₃,Y₂
QuadReg L₁,L₄,Y₃
{0,1}→L₁
{0,J}→L₂
{0,H}→L₃
{0,B}→L₄
LinReg(ax+b) Y₄
LinReg(ax+b) L₁,L₃,Y₅
LinReg(ax+b) L₁,L₄,Y₆

This stuff is missing the point of using QuadReg completely.... If you're doing it this many times, it might be worth considering a loop with a linreg in it, or if you want to try real hard, go with QuartReg and get a quartic function to do 5 at at a time but these "all at once" things don't seem to be of your liking Razz

I did that because I didn't know how to get J and H out of the string because of what comes before it. Is there a way to delete characters at the start of the string?
Quote:


Code:
If fPart(Z/10)=9

By definition, the fPart of a number will never be bigger than 0.99999999... so this conditional will never go through.

I did this because Z holds the row value for output(). So if the fraction of Z/10 is 9, that means we are on row 9.
Quote:

Code:
seq(C nCr X,X,0,C)→L₆
Disp L₆

Here you don't reuse L₆, so you could just do

Code:
Disp seq(C nCr X,X,0,C

Makes sense.
Quote:
Here, you could use the Prompt command...
Code:
Input "A=",A
Input "B=",B
Input "D=",D

could be

Code:
Prompt A,B,D

The only difference would be that it would say "A=?" instead of "A="

I've heard that Input was a better command than Prompt. Is there a difference?
Quote:

Code:
(Y-26)+1→Y
could be
Y-25→Y
this doesn't make much of a difference but I try to keep things simplified...

Will do.
seanlego23 wrote:
I did that because I didn't know how to get J and H out of the string because of what comes before it. Is there a way to delete characters at the start of the string?
Sure, just some tricks with sub()
seanlego23 wrote:
I did this because Z holds the row value for output(). So if the fraction of Z/10 is 9, that means we are on row 9.
Just take a simple look at TI-Basic Developer. frac() outputs the fractional part of a number/division. That number is always in the range [0,0.99999999999], and never 9. The fraction of Z/10 is one of these values: 0, 0.1, 0.2 ... 0.9, and never 9.
seanlego23 wrote:

Code:
seq(C nCr X,X,0,C)→L₆
Disp L₆
Change this to
Code:
Pause seq(C nCr X,X,0,C

seanlego23 wrote:
I've heard that Input was a better command than Prompt. Is there a difference?
For the results: no, but with Input you can display another string, which is sometimes useful.

Side question: can we assume A, B and D are integers?
Okay, since you are doing this on a CE, I'd recommend just using ToString CE by MateoConLechuga instead of pure basic. This will be much faster and allow for you to save a ton of bytes. Besides, although it has not yet been officially announced (I think?) the command should come out in the next OS revision.
  
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 2
» 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