QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: FellippeHeitor on March 15, 2019, 10:37:15 am

Title: Visualizing _PI challenge
Post by: FellippeHeitor on March 15, 2019, 10:37:15 am
Anyone would be willing to take on the challenge to recreate this or even reinvent it? https://www.reddit.com/r/dataisbeautiful/comments/b13h1d/adding_to_the_pi_visualizations_oc/
Title: Re: Visualizing _PI challenge
Post by: bplus on March 15, 2019, 11:02:41 am
Hi Fellippe,

Is there some sort of calculation of Pi going on or is this just bouncing from digit to digit of a Pi expansion?

Repeating digits could use a little loop, internal or external.
Title: Re: Visualizing _PI challenge
Post by: Qwerkey on March 15, 2019, 11:37:32 am
... And when you have taken up this challenge, unless you produce pi to more than 31 trillion digits, we shall think that you have hardly tried at all.  (To any mathematicians out there, why do mathematicians do that sort of thing and who knows if they're correct?).
Title: Re: Visualizing _PI challenge
Post by: Petr on March 15, 2019, 12:28:29 pm
It looks interesting. To calculate pi, we could use the formula π = 2 x (2 x 2 x 4 x 4 x 6 x 6 ...) / (1 x 3 x 3 x 5 x 5 x 7 ...)
but a sample example uses only a few decimal places to visualize ...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. TYPE nrs
  4.     x AS INTEGER
  5.     y AS INTEGER
  6. DIM N(10) AS nrs
  7.  
  8. CONST CX = 400
  9. CONST CY = 300
  10. pi$ = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273"
  11.  
  12. FOR p = -60 TO 264 STEP 36
  13.     q = _D2R(p)
  14.     N(w).x = CX + COS(q) * 150
  15.     N(w).y = CY + SIN(q) * 150
  16.     w = w + 1
  17.     CIRCLE (CX + COS(q) * 150, CY + SIN(q) * 150), 3
  18.     _PRINTSTRING (CX + COS(q) * 170, CY + SIN(q) * 170), _TRIM$(STR$(num))
  19.     num = num + 1
  20.  
  21. CIRCLE (400, 300), 150
  22.  
  23. FOR sh = 1 TO LEN(pi$)
  24.     IF ASC(pi$, sh) >= 48 AND ASC(pi$, sh) <= 57 THEN
  25.         v$ = MID$(pi$, sh, 1)
  26.         LINE (ox, oy)-(ox2, oy2), _RGB32(127, 127, 127)
  27.          i = ASC(pi$, sh) - 48
  28.         LOCATE 1, 1: PRINT MID$(pi$, 1, sh)
  29.         ox = N(oldN).x: oy = N(oldN).y: ox2 = N(i).x: oy2 = N(i).y
  30.         LINE (N(oldN).x, N(oldN).y)-(N(i).x, N(i).y), _RGB32(255, 255, 255): oldN = i
  31.         oldN = i
  32.         _DELAY .2
  33.     END IF
  34. NEXT sh
  35.  
Title: Re: Visualizing _PI challenge
Post by: Petr on March 15, 2019, 12:46:30 pm
There is one more way - the infinite division using MOD, but the fraction 22/7 is inaccurate, because there is a wrong result in the third decimal place. Another fraction is 355/113, but there will also be an error in the seventh decimal place.
Title: Re: Visualizing _PI challenge
Post by: FellippeHeitor on March 15, 2019, 12:53:51 pm
Wow, that was fast, Petr. Good job!

@bplus
I don't know if they're calculating as they go or if it's just fetching from a precalculated list of digits. I just liked the visualization very much when I fist came across it.

@Qwerkey
It's indeed silly to try to break any supercomputer's record in findind the next digits of pi... Maybe mathematicians just wanna have fun in the end too (and measure dick).

I'm looking to take part in the challenge myself. I'll keep this thread as a post-it reminder for me to do it eventually.
Title: Re: Visualizing _PI challenge
Post by: Petr on March 15, 2019, 12:58:30 pm
Thank you Fellippe, it was a welcome change, such a rebound from the 3D maze editor that I am writing.
Title: Re: Visualizing _PI challenge
Post by: jack on March 15, 2019, 01:08:36 pm
I think the Spigot algorithm would be perfect for this https://rosettacode.org/wiki/Pi
Title: Re: Visualizing _PI challenge
Post by: Pete on March 15, 2019, 01:15:32 pm
I haven't figured out how far my string math routines would go, I just know if it was accurate and infinite, it would take about 40 years to surpass the record pi calculation to date, of 31.4 trillion digits. Anyone know where I can find a computer that will last 40 years, without a %^&^$# Windows update?

Pete
Title: Re: Visualizing _PI challenge
Post by: bplus on March 15, 2019, 02:14:22 pm
Hey Petr, nice and fast.

I had basic scheme worked out when I saw you had already posted. So I better have some better features!

I do. :)

This draws an outer loop around digit when digit repeats sequentially (you have to wait awhile for 7), plus it has the pointer moving between points...
Code: QB64: [Select]
  1. _TITLE "Visualizing Pi" 'B+ 2019-03-15
  2.  
  3. 'Featuring a circle loop around the a repeated digit.
  4. ' 3 first, 8, 9, 4, 1, 6, 2, 5, 0 (300+ digits), 7 (560 digits) EDIT
  5.  
  6. 'ref  http://www.math.com/tables/constants/pi.htm  First 100 digits
  7. CONST pi100 = "3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
  8. 'ref same as above 1000 digits, see if can circle all digits pi shortened to 560 digits
  9. CONST pi = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277"
  10. 'PRINT INSTR(pi, "77") ' last number to get circled for repeating at 560
  11.  
  12. CONST xmax = 800
  13. CONST ymax = 720
  14. CONST x0 = 400
  15. CONST y0 = 420
  16. CONST radius = 240
  17. CONST r2 = 20
  18.  
  19. TYPE vectorType
  20.     x AS SINGLE
  21.     y AS SINGLE
  22.  
  23. SCREEN _NEWIMAGE(xmax, ymax, 32)
  24. _SCREENMOVE 300, 20
  25. s2& = _NEWIMAGE(xmax, ymax, 32) 'to update drawing lines in isolation
  26. t = TIMER(.001)
  27. ca = _PI(2 / 10)
  28. DIM vdigit(0 TO 9) AS vectorType
  29. DIM outer(0 TO 9) AS vectorType
  30. CIRCLE (x0, y0), radius
  31. FOR i = 0 TO 9
  32.     vdigit(i).x = x0 + radius * COS(ca * i - 2 * ca)
  33.     vdigit(i).y = y0 + radius * SIN(ca * i - 2 * ca)
  34.     CIRCLE (vdigit(i).x, vdigit(i).y), 2
  35.     x = x0 + (radius + 18) * COS(ca * i - 2 * ca) - 2
  36.     y = y0 + (radius + 18) * SIN(ca * i - 2 * ca) - 6
  37.     _PRINTSTRING (x, y), LTRIM$(STR$(i))
  38.     outer(i).x = x0 + (radius + r2) * COS(ca * i - 2 * ca)
  39.     outer(i).y = y0 + (radius + r2) * SIN(ca * i - 2 * ca)
  40. _PUTIMAGE , 0, s2&
  41.  
  42. digitPointer = 1
  43. cx = x0: cy = y0: speed = 10
  44. WHILE digitPointer <= LEN(pi)
  45.     d = VAL(MID$(pi, digitPointer, 1))
  46.     targetx = vdigit(d).x: targety = vdigit(d).y
  47.     targetangle = _ATAN2(targety - cy, targetx - cx)
  48.     rc~& = _RGB32(255 * RND, 255 * RND, 255 * RND)
  49.     IF lastd = d THEN
  50.         _DEST s2&
  51.         CIRCLE (outer(d).x, outer(d).y), r2, rc~&
  52.         _DEST 0
  53.         CLS
  54.         _PUTIMAGE , s2&, 0
  55.         CIRCLE (cx, cy), 2, &HFFFFFFFF, BF
  56.         LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer - 1)
  57.         _DISPLAY
  58.     ELSE
  59.         DO
  60.             dist = SQR((targetx - cx) ^ 2 + (targety - cy) ^ 2)
  61.             IF dist < speed THEN
  62.                 newX = targetx
  63.                 newY = targety
  64.             ELSE
  65.                 newX = cx + speed * COS(targetangle)
  66.                 newY = cy + speed * SIN(targetangle)
  67.             END IF
  68.             _DEST s2&
  69.             LINE (cx, cy)-(newX, newY), rc~&
  70.             _DEST 0
  71.             CLS
  72.             _PUTIMAGE , s2&, 0
  73.             CIRCLE (newX, newY), 2, &HFFFFFFFF, BF
  74.             cx = newX: cy = newY
  75.             LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer - 1)
  76.             _DISPLAY
  77.             _LIMIT 60
  78.         LOOP UNTIL cx = targetx AND cy = targety
  79.     END IF
  80.     digitPointer = digitPointer + 1
  81.     lastd = d
  82. LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer)
  83. PRINT TIMER(.001) - t; " secs"
  84.  


Title: Re: Visualizing _PI challenge
Post by: Petr on March 15, 2019, 02:31:17 pm
Hi BPlus!  It's very nice. I even discovered the BF parameter for Circle with your program, I haven't seen it yet. Motion in circle is also very interesting.
Title: Re: Visualizing _PI challenge
Post by: bplus on March 15, 2019, 02:34:44 pm
Hi BPlus!  It's very nice. I even discovered the BF parameter for Circle with your program, I haven't seen it yet. Motion in circle is also very interesting.

HA! Twice!
Code: QB64: [Select]
  1. CIRCLE (cx, cy), 2, &HFFFFFFFF, BF  
  2.  

I started with a line for a solid pointer, forgot to erase that BF!
Title: Re: Visualizing _PI challenge
Post by: Petr on March 15, 2019, 02:37:40 pm
Quote
I started with a line for a solid pointer, forgot to erase that BF!

Really? I thought I discovered something new! :-D
Title: Re: Visualizing _PI challenge
Post by: jack on March 15, 2019, 04:36:30 pm
hello bplus
here's spigot sub for you
Code: QB64: [Select]
  1. DIM pi(100) AS _BYTE
  2. spigot pi()
  3.  
  4. PRINT LTRIM$(STR$(pi(1))); ".";
  5. FOR i = 2 TO UBOUND(pi)
  6.     PRINT LTRIM$(STR$(pi(i)));
  7.  
  8. SUB spigot (pi() AS _BYTE)
  9.     DIM N AS LONG
  10.     N = UBOUND(pi)
  11.     DIM ln AS LONG
  12.     ln = INT((10 * N) / 3) + 1
  13.     DIM A(0 TO ln - 1) AS LONG
  14.     DIM npi AS LONG
  15.     DIM i AS LONG, j AS LONG, k AS LONG
  16.     npi = 0
  17.     FOR i = 0 TO ln - 1
  18.         A(i) = 2
  19.     NEXT
  20.  
  21.     DIM nines AS LONG
  22.     DIM predigit AS LONG
  23.     nines = 0
  24.     predigit = 0
  25.     FOR j = 1 TO N
  26.         DIM q AS LONG
  27.         q = 0
  28.         FOR i = ln TO 1 STEP -1
  29.             DIM x AS LONG
  30.             x = (10 * A((i - 1))) + (q * i)
  31.             A((i - 1)) = x MOD ((2 * i) - 1)
  32.             q = x \ ((2 * i) - 1)
  33.         NEXT
  34.         A(0) = q MOD 10
  35.         q = q \ 10
  36.  
  37.         IF 9 = q THEN
  38.             nines = nines + 1
  39.         ELSEIF 10 = q THEN
  40.             pi(npi) = predigit + 1: npi = npi + 1
  41.             FOR k = 0 TO nines - 1
  42.                 pi(npi) = 0: npi = npi + 1
  43.             NEXT
  44.             predigit = 0
  45.             nines = 0
  46.         ELSE
  47.             pi(npi) = predigit: npi = npi + 1
  48.             predigit = q
  49.  
  50.             IF 0 <> nines THEN
  51.                 FOR k = 0 TO nines - 1
  52.                     pi(npi) = 9: npi = npi + 1
  53.                 NEXT
  54.                 nines = 0
  55.             END IF
  56.         END IF
  57.     NEXT
  58.     pi(npi) = predigit
  59.  
Title: Re: Visualizing _PI challenge
Post by: bplus on March 15, 2019, 05:53:27 pm
Thanks Jack I will check it out.

Update: That compares very well with the reference Pi I was using. I used the following code as a check:
Code: QB64: [Select]
  1. 'from Jack 2019-03-15 QB64 forum "Visualizing Pi" B+ mod to check Pi with reference Pi.
  2. _TITLE "Compare Spigot to Pi Ref digit by digit"
  3. SCREEN _NEWIMAGE(600, 720, 32)
  4. _SCREENMOVE 300, 20
  5. CONST piRef = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277"
  6. DIM pi%(561)
  7. spigot pi%()
  8.  
  9. FOR i = 1 TO UBOUND(pi%)
  10.     PRINT i; " digit spigot vrs ref "; pi%(i); " vrs "; MID$(piRef, i, 1)
  11.     IF i MOD 30 = 29 THEN
  12.         INPUT "OK, press enter "; wate$
  13.         CLS
  14.     END IF
  15.  
  16. SUB spigot (pi%())
  17.     DIM N%
  18.     N% = UBOUND(pi%)
  19.     DIM ln AS LONG
  20.     ln = INT((10 * N%) / 3) + 1
  21.     DIM A(0 TO ln - 1) AS LONG
  22.     DIM npi AS LONG
  23.     DIM i AS INTEGER, j AS INTEGER, k AS INTEGER
  24.     npi = 0
  25.     FOR i = 0 TO ln - 1
  26.         A(i) = 2
  27.     NEXT
  28.  
  29.     DIM nines AS LONG
  30.     DIM predigit AS LONG
  31.     nines = 0
  32.     predigit = 0
  33.     FOR j = 1 TO N%
  34.         DIM q AS LONG
  35.         q = 0
  36.         FOR i = ln TO 1 STEP -1
  37.             DIM x AS LONG
  38.             x = (10 * A((i - 1))) + (q * i)
  39.             A((i - 1)) = x MOD ((2 * i) - 1)
  40.             q = x \ ((2 * i) - 1)
  41.         NEXT
  42.         A(0) = q MOD 10
  43.         q = q \ 10
  44.  
  45.         IF 9 = q THEN
  46.             nines = nines + 1
  47.         ELSEIF 10 = q THEN
  48.             pi%(npi) = predigit + 1: npi = npi + 1
  49.             FOR k = 0 TO nines - 1
  50.                 pi%(npi) = 0: npi = npi + 1
  51.             NEXT
  52.             predigit = 0
  53.             nines = 0
  54.         ELSE
  55.             pi%(npi) = predigit: npi = npi + 1
  56.             predigit = q
  57.  
  58.             IF 0 <> nines THEN
  59.                 FOR k = 0 TO nines - 1
  60.                     pi%(npi) = 9: npi = npi + 1
  61.                 NEXT
  62.                 nines = 0
  63.             END IF
  64.         END IF
  65.     NEXT
  66.     pi%(npi) = predigit
  67.  
  68.  
Title: Re: Visualizing _PI challenge
Post by: Jack002 on March 15, 2019, 07:38:19 pm
I haven't figured out how far my string math routines would go, I just know if it was accurate and infinite, it would take about 40 years to surpass the record pi calculation to date, of 31.4 trillion digits. Anyone know where I can find a computer that will last 40 years, without a %^&^$# Windows update?

Pete
That computer Deep Thought in Hitchhiker's Guide to the Galaxy never had these problems. :-)
 
Title: Re: Visualizing _PI challenge
Post by: jack on March 15, 2019, 08:14:29 pm
in the spigot routine, if the dimension in pi is greater than 9830 then I get an unhandled error #9, subscription out of range, anybody know why?
not that anybody would use that routine to calculate pi to that many places.
Title: Re: Visualizing _PI challenge
Post by: codeguy on March 15, 2019, 08:57:24 pm
Wasn't certain where this arbitrary precision Pi calculation algorithm should go.
Code: QB64: [Select]
  1. DECLARE SUB atan239 (denom&)
  2. DECLARE SUB atan5 (denom&)
  3. DECLARE SUB PrintOut (words%)
  4.  
  5. 'Program to calculate pi, version 4.8
  6. 'A major rewrite of version 4.2, this uses only two arrays instead of
  7. 'three, and includes a host of speedups based on a similar C program.
  8. 'A sampler: all the carries are reserved until the end, the divide and
  9. 'add routines are combined, two terms are added at a time, and the number
  10. 'of function calls is minimized. It's a big change for a small gain, since
  11. 'the compiled version requires 28.6 seconds for 5000 digits on my 486 66MHz
  12. 'computer, a 10% gain over version 4.2; like before, it's capable of about
  13. '150,000 digits of pi.
  14. '
  15. 'This program has come a long way from version 1.0; thanks are due to
  16. 'Larry Shultis, Randall Williams, Bob Farrington and Adrian Umpleby.
  17. 'One final note for speed freaks: this program will run about 6 times faster
  18. 'if written in C using an optimizing compiler. Likewise, if you can figure
  19. 'out a way to do integer division and use both the quotient and remainder,
  20. 'this program can easily be sped up by 25%.      -jasonp@isr.umd.edu
  21.  
  22. DEFINT A-Z
  23. INPUT "how many digits"; digits&
  24.  
  25. words = digits& \ 4 + 3
  26. DIM SHARED sum&(words + 1), term(words + 1)
  27. start! = TIMER
  28.                                        
  29.                                          '---------------16*atan(1/5)
  30. denom& = 3: firstword = 1: lastword = 2
  31. sum&(1) = 3: term(1) = 3: sum&(2) = 2000: term(2) = 2000
  32.  
  33. DO UNTIL firstword >= words
  34.    CALL atan5(denom&)
  35.    denom& = denom& + 2
  36.  
  37.                                          '------------   -4*atan(1/239)
  38. denom& = 3: firstword = 2: remainder& = 4
  39.  
  40. FOR x = 2 TO words
  41.    dividend& = remainder& * 10000              'crunch out 1st term
  42.    term(x) = dividend& \ 239&
  43.    remainder& = dividend& - term(x) * 239&
  44.    sum&(x) = sum&(x) - term(x)
  45.  
  46. DO UNTIL firstword >= words
  47.    CALL atan239(denom&)
  48.    denom& = denom& + 4
  49.                                      
  50.  
  51. FOR x = words TO 2 STEP -1                        '-------finish up
  52.    IF sum&(x) < 0 THEN                                  'release carries
  53.       quotient& = sum&(x) \ 10000                       'and borrows
  54.       sum&(x) = sum&(x) - (quotient& - 1) * 10000
  55.       sum&(x - 1) = sum&(x - 1) + quotient& - 1
  56.    END IF
  57.    IF sum&(x) >= 10000 THEN
  58.       quotient& = sum&(x) \ 10000
  59.       sum&(x) = sum&(x) - quotient& * 10000
  60.       sum&(x - 1) = sum&(x - 1) + quotient&
  61.    END IF
  62.  
  63. CALL PrintOut(words)
  64. PRINT "computation time: "; TIMER - start!; " seconds"
  65.  
  66. '------------------------------------------------------------------
  67. SUB atan239 (denom&)
  68. SHARED words, firstword
  69.  
  70. remainder1& = term(firstword)                        'first divide implicitly
  71. remainder2& = 0: remainder3& = 0: remainder4& = 0
  72. denom2& = denom& + 2: firstword = firstword + 1
  73.  
  74. FOR x = firstword TO words
  75.    temp& = term(x)
  76.    dividend& = remainder1& * 10000 + temp&
  77.    temp& = dividend& \ 57121
  78.    remainder1& = dividend& - temp& * 57121
  79.  
  80.    dividend& = remainder2& * 10000 + temp&
  81.    temp2& = dividend& \ denom&
  82.    remainder2& = dividend& - temp2& * denom&
  83.    sum&(x) = sum&(x) + temp2&
  84.  
  85.    dividend& = remainder3& * 10000 + temp&
  86.    temp& = dividend& \ 57121
  87.    remainder3& = dividend& - temp& * 57121
  88.  
  89.    dividend& = remainder4& * 10000 + temp&
  90.    temp2& = dividend& \ denom2&
  91.    remainder4& = dividend& - temp2& * denom2&
  92.    sum&(x) = sum&(x) - temp2&
  93.    term(x) = temp&
  94.  
  95. firstword = firstword + 1
  96. IF term(firstword) = 0 THEN firstword = firstword + 1
  97.  
  98.  
  99. '-------------------------------------------------------------------
  100. SUB atan5 (denom&)
  101. SHARED words, firstword, lastword
  102.  
  103. FOR x = firstword TO lastword + 1
  104.    temp& = term(x)
  105.    dividend& = remainder1& * 10000 + temp&
  106.    temp& = dividend& \ 25
  107.    remainder1& = dividend& - temp& * 25&
  108.    term(x) = temp&
  109.  
  110.    dividend& = remainder2& * 10000 + temp&
  111.    temp& = dividend& \ denom&
  112.    remainder2& = dividend& - temp& * denom&
  113.    sum&(x) = sum&(x) - temp&
  114.  
  115. FOR x = lastword + 2 TO words
  116.    dividend& = remainder2& * 10000
  117.    temp& = dividend& \ denom&
  118.    remainder2& = dividend& - temp& * denom&
  119.    sum&(x) = sum&(x) - temp&
  120.  
  121. IF term(lastword + 1) > 0 AND lastword < words THEN lastword = lastword + 1
  122. IF term(firstword) = 0 THEN firstword = firstword + 1
  123.  
  124. denom& = denom& + 2
  125. remainder1& = 0: remainder2& = 0
  126.  
  127. FOR x = firstword TO lastword + 1
  128.    temp& = term(x)
  129.    dividend& = remainder1& * 10000 + temp&
  130.    temp& = dividend& \ 25
  131.    remainder1& = dividend& - temp& * 25&
  132.    term(x) = temp&
  133.  
  134.    dividend& = remainder2& * 10000 + temp&
  135.    temp& = dividend& \ denom&
  136.    remainder2& = dividend& - temp& * denom&
  137.    sum&(x) = sum&(x) + temp&
  138.  
  139. FOR x = lastword + 2 TO words
  140.    dividend& = remainder2& * 10000
  141.    temp& = dividend& \ denom&
  142.    remainder2& = dividend& - temp& * denom&
  143.    sum&(x) = sum&(x) + temp&
  144.  
  145. IF term(lastword + 1) > 0 AND lastword < words THEN lastword = lastword + 1
  146. IF term(firstword) = 0 THEN firstword = firstword + 1
  147.  
  148.  
  149. '------------------------------------------------------------------
  150. SUB PrintOut (words)
  151. PRINT "pi = 3+."
  152. FOR i = 1 TO words \ 3
  153.    PRINT " ";
  154.    PRINT RIGHT$("0000" + LTRIM$(STR$(sum&(3 * (i - 1) + 2))), 4);
  155.    PRINT RIGHT$("0000" + LTRIM$(STR$(sum&(3 * (i - 1) + 3))), 4);
  156.    PRINT RIGHT$("0000" + LTRIM$(STR$(sum&(3 * (i - 1) + 4))), 4);
  157.    IF i MOD 5 = 0 THEN PRINT "  :"; 12 * i
  158.  
  159. PRINT " ";
  160. FOR i = 3 * (words \ 3) + 2 TO digits
  161.    PRINT RIGHT$("0000" + LTRIM$(STR$(sum&(i))), 4);
  162.  
  163. [code=qb64]
  164.  
Title: Re: Visualizing _PI challenge
Post by: Pete on March 15, 2019, 09:28:23 pm
in the spigot routine, if the dimension in pi is greater than 9830 then I get an unhandled error #9, subscription out of range, anybody know why?
not that anybody would use that routine to calculate pi to that many places.

Sorry, never saw the movie, if there was one, or read the book, but great, now I miss Blondie. :(

Pete
Title: Re: Visualizing _PI challenge
Post by: Pete on March 15, 2019, 09:32:00 pm
in the spigot routine, if the dimension in pi is greater than 9830 then I get an unhandled error #9, subscription out of range, anybody know why?
not that anybody would use that routine to calculate pi to that many places.

Sure, i is dim as an integer and at that many iterations. i surpasses the 32767 limit. QB then returns a -32768 for i, which throws the subscript out of range error. Try DIM as _INTEGER64 instead of INTEGER for all dimension integers.

Pete
Title: Re: Visualizing _PI challenge
Post by: doppler on March 16, 2019, 05:40:17 pm
My PI challenge is

Apple
Blue berry
Cherry
and sometimes Peach.

Sorry I can't resist.
Title: Re: Visualizing _PI challenge
Post by: Pete on March 16, 2019, 06:02:56 pm
Q: What's the difference between a round pi and a square pie? A: $6.72.

A round pi is 3.14, and a square pi is 9.86

Ah, pi humor. And now back to our regularly scheduled program-ing...

Pete
Title: Re: Visualizing _PI challenge
Post by: jack on March 17, 2019, 03:27:46 am
thanks Pete :)
Title: Re: Visualizing _PI challenge
Post by: Bert22306 on March 17, 2019, 09:29:50 pm
So, pi humor aside, I thought searching for a fraction of two rational integers that approximates pi was kind of interesting. Of course, a fraction of two rational integers will always be a rational number, so it'll never be exact. So I wrote a search routine, and here are the first few results. I won't list the intermediate results except for the first two, just to show how the routine works. (Not terribly efficient, since I see the value 3.14 showing up repeatedly, as I increase the numerator or the denominator, in that last trial especially.)

Turns out, 355/113 is a pretty decent rational approximation! You have to go a lot ,longer to gain an extra decimal of accuracy.

Actual pi = 3.141592653589793238....

Iteration 1    1 / 1 = 1
Iteration 2    2 / 1 = 2
Iteration 3    3 / 1 = 3
Accuracy requested = 1
-------------------------------------
Iteration 1    1 / 1 = 1
Iteration 2    2 / 1 = 2
Iteration 3    3 / 1 = 3
Iteration 4    4 / 1 = 4
Iteration 5    4 / 2 = 2
Iteration 6    5 / 2 = 2.5
Iteration 7    6 / 2 = 3
Iteration 8    7 / 2 = 3.5
Iteration 9    7 / 3 = 2.333333333333333
Iteration 10   8 / 3 = 2.666666666666667
Iteration 11   9 / 3 = 3
Iteration 12   10 / 3 = 3.333333333333333
Iteration 13   10 / 4 = 2.5
Iteration 14   11 / 4 = 2.75
Iteration 15   12 / 4 = 3
Iteration 16   13 / 4 = 3.25
Iteration 17   13 / 5 = 2.6
Iteration 18   14 / 5 = 2.8
Iteration 19   15 / 5 = 3
Iteration 20   16 / 5 = 3.2
Accuracy requested = .1
----------------------------
Iteration 28   22 / 7 = 3.142857142857143
Accuracy requested = .01
----------------------------
Iteration 264   201 / 64 = 3.140625
Accuracy requested = .001
----------------------------
Iteration 438   333 / 106 = 3.141509433962264
Accuracy requested = .0001
----------------------------
Iteration 467   355 / 113 = 3.141592920353983
Accuracy requested = .00001
----------------------------
Iteration 467    355 / 113 = 3.141592920353983
Accuracy requested = .000001
----------------------------
Iteration 100122   75948 / 24175 = 3.141592554291623
Accuracy requested = .0000001