PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] Pascal's triangle-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] Pascal's triangle

  1. #1

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Resolved [RESOLVED] Pascal's triangle

    hi,
    I've done many attempts but still didn't succeed in making a logic for pascal's triangle, I want you guys to get me start this task and I'll code it myself
    looking at Paul's thread, I don't understand how did the Paul do it.
    http://www.vbforums.com/showpost.php...23&postcount=4
    So, i need a logic, how would i start it?


    thanks

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Pascal's triangle

    Well if you look closely you can see the following:
    1
    12
    123
    1234
    12345
    So this is your base. On the other end you see that the inverted version of this is sticked to the one:
    21
    321
    4321
    54321
    So it is somewhat like this:
    1
    left side add 2
    right side add 2
    left side add 3
    right side add 3
    Update number
    left side add 4
    right side add 4
    left side add 5
    right side add 5
    Code:
    Public Function addSides(ByVal input As String, ByVal text As String) As String
        Return StrReverse(text) & input & text
    End Function
    I only have no idea why his code is so long. Is something special supposed to happen? xD

    EDIT

    Note: you need some sort of special formatting before every line to center the 1 correctly.

  3. #3

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: Pascal's triangle

    Quote Originally Posted by bergerkiller View Post
    I only have no idea why his code is so long. Is something special supposed to happen? xD
    Yep. that confused me
    ok, i got take a look at your explanation but in the figure:
    Name:  PascalsTriangle.gif
Views: 3388
Size:  2.7 KB
    sides of triangle would be 1, what about for numbers inside? (that's confusing for me)

    i can understand that numbers adds up in a form of triangle and i can get a new number like: 1+1=2 then 1+2=3, 1+3=4 e.t.c
    first i should make two right triangles and then concatenate them?




    thanks..
    Last edited by Aash; Aug 1st, 2011 at 11:30 AM.

  4. #4
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Pascal's triangle

    Neighbouring


    Positions (array index):


    Do as Paul did: start off with a 2-dimensional array of INTEGERs.
    Code:
    Dim Count As Integer = 5
    Dim grid(Count - 1)(Count - 1) As Integer
    Fill(grid, Count / 2, 0)
    And make a recursive function fill this up.
    Code:
    Public Sub Fill(ByRef input()() As Integer, ByVal x As Integer, ByVal y As Integer)
       'check if input(x - 1, y - 1) is possible!!
       input(x, y) += input(x - 1, y - 1)
       'check if input(x + 1, y - 1) is possible!!
       input(x, y) += input(x + 1, y - 1)
       'neither? set 1
       if input(x, y) = 0 Then input(x, y) = 1
    
       'possible?
        Fill(input, x - 1, y + 1)
       'possible?
        Fill(input, x + 1, y + 1)
    End Sub

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: Pascal's triangle

    Try this:
    vb.net Code:
    1. Private Function CreatePascalTriangle(ByVal numberOfRows As Integer) As List(Of Integer())
    2.         Dim triangle As New List(Of Integer())
    3.         For i As Integer = 0 To numberOfRows - 1
    4.             triangle.Add(New Integer(i) {})
    5.             triangle(i)(0) = 1
    6.             triangle(i)(i) = 1
    7.             If i > 1 Then
    8.                 For j As Integer = 1 To i - 1
    9.                     triangle(i)(j) = triangle(i - 1)(j - 1) + triangle(i - 1)(j)
    10.                 Next
    11.             End If
    12.         Next
    13.         Return triangle
    14.     End Function
    15.  
    16.     Private Sub DisplayPascalTriangle(ByVal triangle As List(Of Integer()))
    17.         Dim midpoint As Integer = triangle.Count
    18.         Dim padding As Integer = 0
    19.         Dim line As String = String.Empty
    20.         Dim arr() As Integer = Nothing
    21.         Dim sb As New System.Text.StringBuilder
    22.         For row As Integer = 0 To triangle.Count - 1
    23.             padding = midpoint - row
    24.             arr = triangle(row)
    25.             For i As Integer = 0 To arr.GetUpperBound(0)
    26.                 If i = 0 Then
    27.                     line = arr(i).ToString.PadLeft(padding) & " "
    28.                 Else
    29.                     line &= arr(i).ToString & " "
    30.                 End If
    31.             Next
    32.             sb.AppendLine(line)
    33.         Next
    34.         Debug.Write(sb.ToString)
    35.     End Sub
    36.  
    37.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    38.         Dim triangle As List(Of Integer()) = CreatePascalTriangle(7)
    39.         DisplayPascalTriangle(triangle)
    40.     End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: Pascal's triangle

    will try it

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width