Author Topic: Visualizing _PI challenge  (Read 6373 times)

0 Members and 1 Guest are viewing this topic.

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Visualizing _PI challenge
« Reply #15 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. :-)
 
QB64 is the best!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Visualizing _PI challenge
« Reply #16 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.
« Last Edit: March 15, 2019, 08:16:13 pm by jack »

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: Visualizing _PI challenge
« Reply #17 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.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Visualizing _PI challenge
« Reply #18 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Visualizing _PI challenge
« Reply #19 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: Visualizing _PI challenge
« Reply #20 on: March 16, 2019, 05:40:17 pm »
My PI challenge is

Apple
Blue berry
Cherry
and sometimes Peach.

Sorry I can't resist.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Visualizing _PI challenge
« Reply #21 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Visualizing _PI challenge
« Reply #22 on: March 17, 2019, 03:27:46 am »
thanks Pete :)

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Visualizing _PI challenge
« Reply #23 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