# a question about pascal triangle

• 10-29-2010, 04:12 PM
rambo126
can you help me about the Pascal triangle with depth (number of rows) 7 as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

how can i do that with using loops in java??
• 10-29-2010, 04:22 PM
JosAH
Quote:

Originally Posted by rambo126
can you help me about the Pascal triangle with depth (number of rows) 7 as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

how can i do that with using loops in java??

You can generate the next row given the current row; let the number of numbers in the current row be n and let 'r' be an array representing the current row. From right to left do this:

Code:

```r[n]= 1; for (int i= n; --i > 0; r[i]+= r[i-1]);```
You have to figure out that for-loop because it's a complete give away.

kind regards,

Jos
• 10-29-2010, 04:35 PM
rambo126
thanks jos but how can i do without using array?
• 10-29-2010, 04:55 PM
JosAH
Quote:

Originally Posted by rambo126
thanks jos but how can i do without using array?

Why don't you want to use an array? A single array of the size equal to the height of the triangle is enough and the computations are extremely cheap. The alternative if that use calculate C(n, k) for each element in row n, element k but that would be much more expensive and involves quite a bit of cleverness to circumvent the obvious limit of 13 (13! is the max number you can store in an int).

kind regards,

Jos
• 10-30-2010, 12:42 AM
rambo126
i want to do it by using loops because i do not have enough information about arrays and also the question asks me solve the problem by using loops.. You said: The alternative if that use calculate C(n, k) for each element in row n, element k. Could you clarify this?
• 10-30-2010, 12:59 AM
pbrockway2
See for example the Wikipedia article on Combination.
• 10-30-2010, 06:31 PM
rambo126
Code:

```import java.util.Scanner;         public class Lab05c{                   public static void main(String args[]){         int m, n, f, mf, nf, nmf, k, result, i;     int maxRow=6;     for( n =0;n<=maxRow;n++){         k=n;         result=1;         for(i=2;i<=k;i++)                 result=result*i         nf=result;         k=m=0;         result=1;         for(i=2;i<=k;i++)                 result=result*i;         mf=result;         k=n-m;         result=1;         for(i=2;i<=k;i++)                 result=result*i;         nmf=result;         f= nf/(mf*nmf);         for( m=0;m<=n;m++)                 System.out.print(f);       System.out.println();     }  } }```

i did like this and the output of this:
1
11
111
1111
11111
111111

what do i need to have right result? can you help me

• 10-30-2010, 07:17 PM
Fubarable
Sorry, but it's kind of hard to follow your code. Myself, if this were my project, I'd subdivide things by creating a small method that calculated the factorial of a number, n! = n * (n -1) * (n - 2)...., say
Code:

```  public static int factorial(int n) {       if (n == 0) {         return 1;       }       //... more code goes here. :-)   }```

And then again, using pbrockway's reference, create a very short method that calculates the combination:
Code:

```  public static int combination(int n, int k) {       // only one line of code here that uses the factorial method above a few times   }```

And then call the combination method inside of a nested for loop, and you're done.

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
Much luck!