|
-
Jul 15th, 2011, 05:25 AM
#1
Thread Starter
Hyperactive Member
-
Jul 15th, 2011, 06:08 AM
#2
Fanatic Member
Re: Pascal's triangle
Well if you look closely you can see the following:
So this is your base. On the other end you see that the inverted version of this is sticked to the one:
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.
Last edited by bergerkiller; Jul 15th, 2011 at 06:12 AM.
-
Jul 15th, 2011, 06:39 AM
#3
Thread Starter
Hyperactive Member
Last edited by Aash; Aug 1st, 2011 at 11:30 AM.
-
Jul 15th, 2011, 08:57 AM
#4
Fanatic Member
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
Last edited by bergerkiller; Jul 15th, 2011 at 09:28 AM.
-
Jul 15th, 2011, 10:29 PM
#5
Re: Pascal's triangle
Try this:
vb.net Code:
Private Function CreatePascalTriangle(ByVal numberOfRows As Integer) As List(Of Integer()) Dim triangle As New List(Of Integer()) For i As Integer = 0 To numberOfRows - 1 triangle.Add(New Integer(i) {}) triangle(i)(0) = 1 triangle(i)(i) = 1 If i > 1 Then For j As Integer = 1 To i - 1 triangle(i)(j) = triangle(i - 1)(j - 1) + triangle(i - 1)(j) Next End If Next Return triangle End Function Private Sub DisplayPascalTriangle(ByVal triangle As List(Of Integer())) Dim midpoint As Integer = triangle.Count Dim padding As Integer = 0 Dim line As String = String.Empty Dim arr() As Integer = Nothing Dim sb As New System.Text.StringBuilder For row As Integer = 0 To triangle.Count - 1 padding = midpoint - row arr = triangle(row) For i As Integer = 0 To arr.GetUpperBound(0) If i = 0 Then line = arr(i).ToString.PadLeft(padding) & " " Else line &= arr(i).ToString & " " End If Next sb.AppendLine(line) Next Debug.Write(sb.ToString) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim triangle As List(Of Integer()) = CreatePascalTriangle(7) DisplayPascalTriangle(triangle) 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 -
-
Jul 16th, 2011, 11:58 PM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|