Results 1 to 8 of 8
  1. #1
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default a question about pascal triangle

    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??

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rambo126 View Post
    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:

    Java 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

  3. #3
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    thanks jos but how can i do without using array?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rambo126 View Post
    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

  5. #5
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    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?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    See for example the Wikipedia article on Combination.

  7. #7
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Java 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

    Moderator Edit: code tags added
    Last edited by Fubarable; 10-30-2010 at 07:56 PM. Reason: Moderator Edit: code tags added

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java 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:
    Java 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:

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

Similar Threads

  1. pascal to java converter
    By j2me64 in forum Forum Lobby
    Replies: 1
    Last Post: 04-17-2010, 10:56 PM
  2. Pascal Triangle noob help
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 02-17-2009, 01:05 AM
  3. Translating Pascal into java
    By willemjav in forum Advanced Java
    Replies: 120
    Last Post: 08-16-2008, 12:26 AM
  4. PASCAL & JAVA? for senior members
    By willemjav in forum Forum Lobby
    Replies: 2
    Last Post: 07-13-2008, 12:11 PM
  5. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •