Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pascal Triangle
#1
Really simple starter:
Code: (Select All)
While 1
    Cls
    For i = 1 To 5
        If i = 1 Then Pascal = i Else Pascal = Pascal * 11
        P$ = LTrim$(Str$(Pascal))
        For j = 1 To Len(P$)
            Locate i, (5 - i) * 3 + j * 3 + (2 - (cnt Mod 2)) * i: Print Mid$(P$, j, 1);
        Next
        Print
    Next
    cnt = cnt + 1
    _Limit 2
Wend

To get warmed up.

More serious display exercise:
Code: (Select All)
_Title "Pascal Triangle display exercise 2018-01-13 bplus"
'2018-01-13 Pascal Triangle.txt for JB 2015-10-31 MGA/B+

_Define A-Z As _INTEGER64

Const xmax = 1200
Const ymax = 400

Screen _NewImage(xmax, ymax, 32)
_ScreenMove 100, 60

printline = 2
For row = 0 To 20
    build$ = ""
    printline = printline + 1
    For column = 0 To row
        build$ = build$ + Right$(Space$(7) + Str$(binom(row, column)), 7)
    Next
    Locate printline, (150 - Len(build$)) / 2
    Print build$
Next
Sleep

Function binom (n, m)
    binom = fac(n) / (fac(m) * fac((n - m)))
End Function

Function fac (n)
    f = 1
    For i = 1 To n
        f = f * i
    Next
    fac = f
End Function
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#2
Now for some fun!

Odd = "* " or even = space "  "

Code: (Select All)
_Title "Pascal Triangle display exercise 2025-11-29 update bplus"
'2018-01-13 Pascal Triangle.txt for JB 2015-10-31 MGA/B+
' 2025-11-29 center triangle
_Define A-Z As _INTEGER64

Const xmax = 600
Const ymax = 400

Screen _NewImage(xmax, ymax, 32)
_ScreenMove 100, 60

printline = 2
For row = 0 To 20
    build$ = ""
    printline = printline + 1
    For column = 0 To row
        If binom(row, column) Mod 2 Then build$ = build$ + "* " Else build$ = build$ + "  "
    Next

    Locate printline, (80 - Len(build$)) / 2
    Print build$
Next
Sleep

Function binom (n, m)
    binom = fac(n) / (fac(m) * fac((n - m)))
End Function

Function fac (n)
    f = 1
    For i = 1 To n
        f = f * i
    Next
    fac = f
End Function

Look familiar Sierpinski?
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#3
We are not alone.

Code: (Select All)
_Title "Pascal Triangle Aliens Among Us" '2022-10-10 bplus

_Define A-Z As _INTEGER64
Screen _NewImage(500, 500, 32)
_ScreenMove 350, 100

For row = 0 To 20 ' level on pyramind 0 is 1, 1 is 1,1  2 is 1,2,1...
    Print "Row"; row
    For col = 0 To row
        b$ = Dec2Bin$(binom(row, col))
        'Print b$; ",";
        h = (1 + Len(b$)) * 20
        For y = 1 To Len(b$)
            If Mid$(b$, y, 1) = "1" Then Line (250 - 10 * row + col * 20, 500 - h + y * 20)-Step(20, 20), _RGB32(0, 180, 0), BF
        Next
    Next
    Print "press any..."
    Sleep
    Cls
Next

Function binom (n, m)
    binom = fac(n) / (fac(m) * fac((n - m)))
End Function

Function fac (n)
    f = 1
    For i = 1 To n
        f = f * i
    Next
    fac = f
End Function

Function Dec2Bin$ (integerBase10 As _Integer64) 'no more &B because easier to add than to remove
    Dim j As Integer, b$
    If integerBase10 = 0 Then Dec2Bin$ = "0": Exit Function
    While 2 ^ j <= integerBase10
        If (integerBase10 And 2 ^ j) > 0 Then b$ = "1" + b$ Else b$ = "0" + b$
        j = j + 1
    Wend
    Dec2Bin$ = b$
End Function

We have never been alone.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Triangle Dissection bplus 1 793 05-05-2022, 04:01 AM
Last Post: Pete

Forum Jump:


Users browsing this thread: