Author Topic: ASCII Sierpinski Triangle  (Read 5708 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
ASCII Sierpinski Triangle
« on: October 23, 2019, 10:29:20 am »
Hi everyone!
Here is Sierpinski Triangle in SCREEN 0!
I can't make it more bigger because QB64 gives error for 21 factorial (I mean it is out of range for QB64)
Anyway, enjoy this!
Code: QB64: [Select]
  1. '############################################
  2. '# ASCII Sierpinski Triangle By Ashish      #
  3. '#  Fractal is generated with the help of   #
  4. '#         Pascal  Triangle.                #
  5. '#          23 Oct, 2019                    #
  6. '############################################
  7.  
  8.  
  9. _TITLE "ASCII Sierpinski Triangle"
  10. x = _WIDTH / 2 'start from the centre of the screen
  11.  
  12. FOR i = 0 TO 15
  13.     d$ = ""
  14.     LOCATE , x
  15.     FOR j = 0 TO i
  16.         c$ = _TRIM$(STR$(nCr(i, j))) 'calculate the number value of the triangle
  17.         'c$ = "*"
  18.         IF nCr(i, j) MOD 2 = 1 THEN c$ = "*" ELSE c$ = " "
  19.  
  20.         d$ = d$ + c$ + " "
  21.     NEXT
  22.     PRINT d$;
  23.     PRINT
  24.     x = x - 1 'shift each step towards left as the triangle row move down.
  25.  
  26. FUNCTION nCr (n, r) '(n!)/r!*(n-r)!
  27.     IF r > n THEN EXIT FUNCTION
  28.     nCr = fact(n) / (fact(r) * fact(n - r))
  29.  
  30. FUNCTION fact (n)
  31.     fact = 1
  32.     IF n = 0 THEN EXIT FUNCTION
  33.     FOR i = 1 TO n
  34.         fact = fact * i
  35.     NEXT
  36.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #1 on: October 23, 2019, 11:08:16 am »
Hi Ashish,

Very interesting! I did not know Sierpinski could be pulled out of Pascal Triangle.

I did work out a way to get around the huge number problem for nCr (Combination) calcs. I did it for number of 5 card hands you could be dealt from 52, might be able to code generalized solution... I will look into it. But! didn't you post code for finding large factorials? I think I saved it somewhere, guess I will look that up too :)

Update: Found it, it was how many hands of 13 from a deck of 52
Code: QB64: [Select]
  1. _TITLE "How many hands of 13 from a deck" ' B+ started 2019-03-17
  2.  
  3. '   How many hands of 13 from a deck?
  4.  
  5. ' EDIT: formula for Combination of N items taken R at a time = N!/(R! * (N-R)!) so we need
  6. ' 52! / (39! * 13!) =    52 * 51 * 50 * 49 * ... * 40
  7. '                      / 13 * 12 * 11 * ...      * 1
  8.  
  9. DIM a52_39(1 TO 13) AS INTEGER, a13(1 TO 13) AS INTEGER
  10. FOR i = 1 TO 13
  11.     a52_39(i) = 39 + i ' = 40, 41, 42, 43, ... 52 numerator multipliers
  12.     a13(i) = i '             1, 2, 3, 4, ... 13   denominator multipliers
  13.  
  14. ' To keep the numerator from getting too big reduce numerator terms by demoinator terms that divide evenly
  15.  
  16. 'reduce terms in a52_39 by terms in denominator mult 2 to 13
  17. FOR i = 13 TO 2 STEP -1
  18.     found = 0
  19.     FOR j = 1 TO 13 'is the array item divisible by i
  20.         IF a52_39(j) MOD i = 0 THEN a52_39(j) = a52_39(j) \ i: found = 1: EXIT FOR
  21.     NEXT
  22.     IF found = 1 THEN a13(i) = 1 ELSE a13(i) = i
  23.  
  24. ' multiply whats left in numerator and denomiator
  25. f~&& = 1: g = 1
  26. FOR i = 1 TO 13
  27.     f~&& = a52_39(i) * f~&& 'multiple numerators left
  28.     g = a13(i) * g 'multiply denominators left
  29.  
  30. PRINT "The number of hands of 13 in a deck is ";
  31. PRINT USING "###,###,###,###,###.###"; f~&& \ g
  32. PRINT "                                compare to 635,013,559,600"
  33.  

I think the method can be generalized to any combination up to a limit.


And I found your's, FPM: https://www.qb64.org/forum/index.php?topic=1468.msg106680#msg106680
But have to do commands from command line,  :p

Maybe through SHELL can do this with QB64 program?
« Last Edit: October 23, 2019, 11:43:12 am by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #2 on: October 23, 2019, 04:08:46 pm »
Nice Triangle... Oh. A possible use... Vary the colour of each 'star' and overlay it onto an Xmas tree...
Logic is the beginning of wisdom.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #3 on: October 23, 2019, 04:43:03 pm »

Nice work, Ashish.
« Last Edit: October 23, 2019, 04:47:56 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #4 on: October 23, 2019, 08:09:47 pm »
Yes, an alternate way to calculate nCr buys us a bigger triangle, here I have reached the height limit of my screen:
Code: QB64: [Select]
  1. '############################################
  2. '# ASCII Sierpinski Triangle By Ashish      #
  3. '#  Fractal is generated with the help of   #
  4. '#           Pascal  Triangle.              #
  5. '#             23 Oct, 2019                 #
  6. '#  B+ mod of nCr function allows for more. #
  7. '#        Oh, hey try Johnno's idea!        #
  8. '############################################
  9.  
  10. CONST xmax = 800, ymax = 800
  11. SCREEN _NEWIMAGE(xmax, ymax, 32)
  12. _TITLE "ASCII Sierpinski Triangle"
  13. COLOR 0, &HFF004400
  14. x = (_WIDTH / 8) / 2 'start from the centre of the screen
  15. FOR i = 0 TO 47
  16.     d$ = " "
  17.     LOCATE i + 1, x
  18.     FOR j = 0 TO i
  19.         c$ = _TRIM$(STR$(nCr(i, j))) 'calculate the number value of the triangle
  20.         IF nCr(i, j) MOD 2 = 1 THEN c$ = "*" ELSE c$ = " "
  21.         d$ = d$ + c$ + " "
  22.     NEXT
  23.     FOR j = 1 TO LEN(d$)
  24.         c$ = MID$(d$, j, 1)
  25.         IF c$ <> " " THEN
  26.             COLOR _RGB32(RND * 255, RND * 255, RND * 255)
  27.         ELSE
  28.             COLOR , _RGB32(RND * 25, RND * 15 + 20, RND * 25)
  29.         END IF
  30.         PRINT c$;
  31.     NEXT
  32.     'PRINT
  33.     x = x - 1 'shift each step towards left as the triangle row move down.
  34.  
  35. ' B+ alt way to calculate nCr, generalized 2019-10-23
  36. FUNCTION nCr~&& (n AS INTEGER, r AS INTEGER)
  37.     'Note: I am using notes from a successful calc to generalize this method.
  38.     ' How many hands of 13 from a deck?
  39.     ' Formula for Combination of N items taken R at a time = N!/(R! * (N-R)!) so we need
  40.     ' 52! / (39! * 13!) =    52 * 51 * 50 * 49 * ... * 40  / 13 * 12 * 11 * ...      * 1
  41.  
  42.     DIM a AS INTEGER, i AS INTEGER, j AS INTEGER, found AS INTEGER, numerator AS _UNSIGNED _INTEGER64, denomenator AS _UNSIGNED LONG
  43.     a = n - r
  44.     IF a < r THEN r = a 'results are symmteric for r and n-r so use smaller of 2
  45.     a = n - r
  46.  
  47.     DIM numer(1 TO r) AS INTEGER, denom(1 TO r) AS INTEGER
  48.     ' DIM a52_39(1 TO 13) AS INTEGER, a13(1 TO 13) AS INTEGER
  49.     FOR i = 1 TO r
  50.         numer(i) = a + i ' = 40, 41, 42, 43, ... 52 numerator multipliers
  51.         denom(i) = i '     =  1,  2,  3,  4, ... 13 denominator multipliers
  52.     NEXT
  53.  
  54.     ' To keep the numerator from getting too big reduce numerator terms by demoinator terms that divide evenly
  55.     FOR i = r TO 2 STEP -1
  56.         found = 0
  57.         FOR j = 1 TO r 'is the array item divisible by i
  58.             IF numer(j) MOD i = 0 THEN numer(j) = numer(j) \ i: found = 1: EXIT FOR
  59.         NEXT
  60.         IF found = 1 THEN denom(i) = 1
  61.     NEXT
  62.  
  63.     ' multiply whats left in numerator and denomiator
  64.     numerator = 1: denomenator = 1
  65.     FOR i = 1 TO r
  66.         numerator = numerator * numer(i) 'multiple numerators left
  67.         denomenator = denomenator * denom(i) 'multiply denominators left
  68.     NEXT
  69.     nCr~&& = numerator \ denomenator
  70.  
  71.  
« Last Edit: October 23, 2019, 08:30:54 pm by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #5 on: October 24, 2019, 08:16:17 am »
Cool Bplus! Very nice MOD.
I could use FPM, but it is string based. Also, it can give me factorial of any +ve number but the problem comes when you have to divide the quantities.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #6 on: October 24, 2019, 10:04:54 am »
Cool Bplus! Very nice MOD.
I could use FPM, but it is string based. Also, it can give me factorial of any +ve number but the problem comes when you have to divide the quantities.

Ah! the division, yes that would be a problem.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #7 on: October 26, 2019, 03:24:41 am »
Hey Bplus and Ashish...
cool!

PS:
how many slices of math have you at breakfast every day? :-)
Programming isn't difficult, only it's  consuming time and coffee

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #8 on: October 26, 2019, 07:42:48 am »
Hey Bplus and Ashish...
cool!

PS:
how many slices of math have you at breakfast every day? :-)
Not everyday. It's just what taught in school, I make a simple program on that. Factorials, Permutations, Combination, Binomial Theorem,etc are in the syllabus this year. (Also The Calculus Ghost).
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #9 on: October 26, 2019, 10:31:09 am »
I am wondering how someone "saw" Sierpinsky in the odd combinations of Pascal's Triangle, though it's not exactly the Sierpinsky I would draw with lines or dots, still it's dang close!

Ashish, did you "see" that or did someone show you the trick?

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #10 on: October 27, 2019, 01:25:54 am »
I am wondering how someone "saw" Sierpinsky in the odd combinations of Pascal's Triangle, though it's not exactly the Sierpinsky I would draw with lines or dots, still it's dang close!

Ashish, did you "see" that or did someone show you the trick?
I'm not that intelligent. I have known this fact from numberphile's video on Pascal Triangle.

There are also many other amazing properties which I have known from that video.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ASCII Sierpinski Triangle
« Reply #11 on: October 27, 2019, 09:25:40 am »
Oh excellent link, thanks Ashish!