Pascal's triangle: Difference between revisions

Line 336:
===Summing from Previous Rows===
{{works with|Java|1.5+}}
<java>import java.util.LinkedListArrayList;
...//class definition, etc.
public static void genPyrN(int rows){
if(rows < 0) return;
//save the last row here
LinkedListArrayList<Integer> last = new LinkedListArrayList<Integer>();
thisRow last.add(1);
for(int i= 0;i <= rows;++i){
System.out.println(last);
for(int i= 01;i <= rows;++i){
//work on the next row
LinkedListArrayList<Integer> thisRow= new LinkedListArrayList<Integer>();
thisRow.add(1); //beginning
for(int j= 0;j <= i;++j){//loop the number of elements in this row
iffor(jint =j= last.size()1;j ||< i;++j == 0){//ifloop we'rethe onnumber theof elements in this endsrow
//sum from the last row
thisRow.add(1);
}else{//otherwise,thisRow.add(last.get(j sum- from1) the+ last row.get(j));
thisRow.add(last.get(j - 1) + last.get(j));
}
}
thisRow.add(1); //end
last= thisRow;//save this row
System.out.println(thisRow);
Anonymous user