Difference between revisions of "TI-BASIC:Pascal Triangle"

From Learn @ Cemetech
Jump to navigationJump to search
(Initial automated import)
 
(Automated superscript correction)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{{Template:TI-BASIC:Routine
 
{{Template:TI-BASIC:Routine
|picture=@@
+
|picture=
|summary=Returns the ''R''^^th^^ row of Pascal's triangle
+
|summary=Returns the ''R''<sup>th</sup> row of Pascal's triangle
 
|input=''R'' - The requested row of Pascal's triangle
 
|input=''R'' - The requested row of Pascal's triangle
 
|output=''Ans'' - Contains the row, as a list
 
|output=''Ans'' - Contains the row, as a list
 
|variable=R, Ans
 
|variable=R, Ans
 
|compatibility=TI-83/84/+/SE
 
|compatibility=TI-83/84/+/SE
|author=@@
+
|author=
|authors=@@
+
|authors=
|url=@@
+
|url=
 
|download=[file pascaltriangle.zip]
 
|download=[file pascaltriangle.zip]
 
}}
 
}}
Line 30: Line 30:
 
We multiply by 2^R to convert the probabilities to whole numbers. Unfortunately, binompdf( doesn't work for the R=0 case, so we make it a special case (if you never need to compute this case, you can omit this, turning the routine into a single statement).
 
We multiply by 2^R to convert the probabilities to whole numbers. Unfortunately, binompdf( doesn't work for the R=0 case, so we make it a special case (if you never need to compute this case, you can omit this, turning the routine into a single statement).
  
Another way to get the number is to use the [[TI-BASIC:Ncr|NCr]] command. N nCr K returns the K^^th^^ element of the N^^th^^ row (here, K is also numbered starting from 0). This can be done using the following line of code:
+
Another way to get the number is to use the [[TI-BASIC:Ncr|NCr]] command. N nCr K returns the K<sup>th</sup> element of the N<sup>th</sup> row (here, K is also numbered starting from 0). This can be done using the following line of code:
  
 
  :seq(R nCr K,K,0,R
 
  :seq(R nCr K,K,0,R

Latest revision as of 22:23, 24 February 2016


Routine Summary

Returns the Rth row of Pascal's triangle

Inputs

R - The requested row of Pascal's triangle

Outputs

Ans - Contains the row, as a list

Variables Used

R, Ans

Calculator Compatibility

TI-83/84/+/SE

Download

file pascaltriangle.zip

:{1
:If R
:2^Rbinompdf(R,.5

This routine uses the Binompdf( command to generate a row of [wikipedia:Pascal_triangle Pascal's triangle]. Each number in Pascal's triangle is gotten by adding the two numbers above it, like such:

....1 
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

By tradition, the rows are numbered starting with 0, so that 1 is actually the "zeroth" row, while the first row is 1 1.

The binompdf( method of calculating rows of Pascal's triangle relies on one of its many properties: if a coin is flipped R times, then the number of heads (or the number of tails) flipped is distributed in the same way as the rows of the pascal's triangle, and the binompdf( command computes precisely such probabilities.

We multiply by 2^R to convert the probabilities to whole numbers. Unfortunately, binompdf( doesn't work for the R=0 case, so we make it a special case (if you never need to compute this case, you can omit this, turning the routine into a single statement).

Another way to get the number is to use the NCr command. N nCr K returns the Kth element of the Nth row (here, K is also numbered starting from 0). This can be done using the following line of code:

:seq(R nCr K,K,0,R


This goes through a line a gets every value of R nCr K and puts all results into a list. This routine is smaller than the one above, but is also slower.

Error Conditions

  • ERR:DOMAIN is thrown if R is negative, or not a whole number.

Related Routines

If one outputs Pascal's triangle to a graph with even numbers as white dots and odd numbers as black dots, the outcome will be Sierpinski's triangle. Another way to generate Sierpinski's triangle is found below: