Author Topic: Calculate Pi  (Read 4575 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Calculate Pi
« on: August 02, 2019, 07:26:52 pm »
Hi all,

I looked into this a lot today and I know there's the _PI command that has quite a few decimal places, which would be fine in probably any situation. But I wanted to see if it was possible to go beyond that with QB64 and after researching a bit, I found an old BASIC program someone posted on the codecodex wiki page, along with other computer languages to do the same thing, a program that lets the user type in how many decimals they want and it spurts it out from there. When I did it I noticed that the user only sees the bottom portion that's left on the screen, so I added  to it the ability for the user to make Pi.txt that opens automatically at the end and you see it in Notepad (if you have Windows) or whatever default .txt viewer you have. Like I say in the program, I wouldn't go over 20,000 digits though because for that amount, I waited probably around 4 or 5 minutes for it to process it all on my semi-new computer. I also don't know if there's a limit on how much Notepad can hold. This program originally was made and put in the American Mathematical Monthly, it says in 1938 but that is probably just when they thought up a way to figure something like this out. This program is already in 2 places online, here: http://www.codecodex.com/wiki/Calculate_digits_of_pi and here: https://groups.google.com/forum/#!topic/alt.lang.basic/3ZdfRCIihOQ  The second URL is originally from the old Newsgroup alt.lang.basic which I used go to in the 90's sometimes. So anyways, here is what I did with it, enjoy:

Note: Don't use this code, it uses a lot of computer computation without LIMIT. Scroll down to the bottom of this forum topic to get a better version.

Code: QB64: [Select]
  1. 'Almost all of the following code was posted by someone on http://www.codecodex.com/wiki/Calculate_digits_of_pi
  2. 'That was taken from American Mathematical Monthly (see below).
  3. 'I converted it to also make a .txt file of Pi.txt if the user wishes to.
  4.  
  5.  
  6. 'PI.BAS 'Prints PI to number of decimal places desired. 19830623tac.
  7. '        Ref: American Mathematical Monthly vol#45,1938 p657-667.
  8. '        Ref: Best of Micro vol.#1 p85-86.
  9. '
  10. '
  11. '             *       ----                      ----
  12. '        *****         \     16(-1)^(k+1)        \         4(-1)^(k+1)
  13. '       * * *   ---     >   ------------    --    >    ----------------
  14. '         * *   ---    /    (2k-1)5^(2k-1)       /      (2k-1)239^(2k-1)
  15. '         * *         ----                      ----
  16. '        *   *        k=1                       k=1
  17. '
  18. '
  19.  
  20.  
  21. SCREEN _NEWIMAGE(800, 600, 32)
  22. PRINT "                      Calculate Pi"
  23. PRINT "This program will produce Pi to as many digits as you wish."
  24. PRINT "But I do not recommend going over 20000 because it's pretty slow."
  25. PRINT "Do you wish to also have it make a .txt file called Pi.txt"
  26. PRINT "that can be opened with a program such as Windows Notepad?"
  27. PRINT "The program will also open Pi.txt after it is finished making it."
  28. PRINT "If you just see it on the screen, you will only see as much as"
  29. PRINT "the screen can show at the end."
  30. INPUT "Type Yes or No here:"; ff$
  31. IF LEFT$(ff$, 1) = "Y" OR LEFT$(ff$, 1) = "y" THEN file = 1
  32. main:
  33. TEN = 100
  34. INPUT "Enter number of digits wanted"; SIZE:
  35. SIZE = (SIZE + 1) OR 1
  36. PLACES = SIZE - 1
  37.  
  38. IF SIZE > 200 THEN TEN = 10 ELSE SIZE = (SIZE + 1) / 2
  39.  
  40. DIM POWER(SIZE), TERM(SIZE), RESULT(SIZE)
  41. K(1) = 25: K(2) = 239 'Constants
  42. C(1) = 5: C(2) = 239
  43.  
  44. 'Main loop.
  45. FOR PASS = 1 TO 2
  46.  
  47.     Init:
  48.     FOR L = 0 TO SIZE
  49.         POWER(L) = 0: TERM(L) = 0: IF PASS = 1 THEN RESULT = 0
  50.     NEXT L
  51.     POWER(0) = 16 / PASS ^ 2
  52.     MODE = 0
  53.     DIVISOR = C(PASS)
  54.     GOSUB divide
  55.     XPONENT = 1
  56.     SIGN = 3 - 2 * PASS
  57.     '
  58.  
  59.     copy:
  60.     FOR L = 0 TO SIZE: TERM(L) = POWER(L): NEXT L
  61.     '
  62.     MODE = 1
  63.     DIVISOR = XPONENT
  64.     GOSUB divide
  65.  
  66.     MODE = (SIGN < 0) + ABS(SIGN > 0)
  67.     GOSUB addsub
  68.  
  69.     XPONENT = XPONENT + 2
  70.     SIGN = -SIGN
  71.  
  72.     MODE = 0
  73.     DIVISOR = K(PASS)
  74.     GOSUB divide
  75.  
  76.     IF PASS = 2 THEN GOSUB divide
  77.  
  78.     IF ZERO <> 0 THEN GOTO copy
  79.  
  80. NEXT PASS
  81. '
  82. 'Print result.
  83. PRINT "The value of PI to"; PLACES; " decimal places"
  84. PRINT RESULT(0); ".";
  85. IF file = 1 THEN OPEN "pi.txt" FOR OUTPUT AS #1
  86. PRINT #1, "3 . ";
  87. FOR L = 1 TO SIZE
  88.     IF (TEN = 100) * (RESULT(L) < 10) THEN
  89.         PRINT "0";
  90.         PRINT USING "#"; RESULT(L);
  91.         IF file = 1 THEN PRINT #1, "0";
  92.         IF file = 1 THEN PRINT #1, USING "#"; RESULT(L);
  93.         GOTO nxt
  94.     END IF
  95.     IF file = 1 THEN IF L / 30 = INT(L / 30) THEN PRINT #1, " "
  96.     PRINT USING "##"; RESULT(L);
  97.     IF file = 1 THEN PRINT #1, USING "##"; RESULT(L);
  98.     nxt:
  99. IF file = 1 THEN CLOSE #1
  100. IF file = 1 THEN SHELL _DONTWAIT "Pi.txt"
  101.  
  102. divide:
  103. 'Division subroutine.   mode0 =  Power(x)/Div else mode1 =  Term(x)/Div.
  104. DIGIT = 0: ZERO = 0
  105. FOR L1 = 0 TO SIZE
  106.     DIGIT = DIGIT + TERM(L1) * MODE + POWER(L1) - POWER(L1) * MODE
  107.     QUOTIENT = INT(DIGIT / DIVISOR)
  108.     RESIDUE = DIGIT MOD DIVISOR
  109.     ZERO = ZERO OR (QUOTIENT + RESIDUE)
  110.     IF MODE THEN TERM(L1) = QUOTIENT ELSE POWER(L1) = QUOTIENT
  111.     DIGIT = RESIDUE * TEN
  112. NEXT L1
  113. MODE = 0
  114.  
  115. addsub:
  116. 'Add/Subtract subroutine.       mode - 1 =  Subtract  else mode1 = Add.
  117. CARRY = 0
  118. FOR L1 = SIZE TO 0 STEP -1
  119.     SUM = RESULT(L1) + TERM(L1) * MODE + CARRY * MODE: CARRY = 0
  120.     IF (MODE = 1) * (SUM < TEN) + (MODE = -1) * (SUM >= 0) THEN GOTO t1
  121.     SUM = SUM + MODE * -TEN: CARRY = 1
  122.  
  123.     t1:
  124.     RESULT(L1) = SUM
  125. NEXT L1
  126. MODE = 0
  127.  
« Last Edit: August 02, 2020, 12:58:36 am by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #1 on: August 02, 2019, 11:26:28 pm »
I just did it for 100,000 digits and it worked. But it took 39 minutes and I don't suggest this because I don't want to overheat the chip or anything. I suppose someone can add a _LIMIT command on the loop but it would take much longer so it might heat up that way, I don't know. But here is the attachment of the 100,000 Pi digits (actually 100,003 total, I checked it with Word). I renamed it afterward. It is 136 pages long. Check it out!

* pi at 100000 digits - took 39 minutes to process.txt (Filesize: 205.08 KB, Downloads: 158)
« Last Edit: August 02, 2019, 11:39:20 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculate Pi
« Reply #2 on: August 02, 2019, 11:39:48 pm »
LOL so what are you going to do with all those digits?

Here is one thing we did:
https://www.qb64.org/forum/index.php?topic=1176.msg103748#msg103748
« Last Edit: August 02, 2019, 11:43:54 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #3 on: August 02, 2019, 11:58:51 pm »
LOL, well I always wanted to process Pi for a lot of digits. :) Cool program you got there, I ran it  to the end and it said 296.5056 seconds. :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculate Pi
« Reply #4 on: August 03, 2019, 08:39:14 am »
That time is pretty much meaningless since the display was going slow for human eyes to watch, the meaningful statistic is that all the digits repeat after 560 digits, 7 being the last to do so.

Might be interesting to explore the distribution of digits, ie is one digit favored over another in say first 1000 or 10,000 digits ie, ie is the distribution of digits random or does it follow pattern? Would segments of PI make good random number generator?

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #5 on: August 03, 2019, 02:53:54 pm »
B+, I checked my text file to look for those repeating numbers you mentioned and couldn't find them. Then I looked online about it and it says since it's an irrational number, Pi never repeats. You can read about it here:
https://www.askamathematician.com/2013/12/q-how-do-we-know-that-%CF%80-never-repeats-if-we-find-enough-digits-isnt-it-possible-that-it-will-eventually-start-repeating/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculate Pi
« Reply #6 on: August 03, 2019, 03:42:14 pm »
By repeat here, I just mean two in a row like 2 and then 2 again right after last 2.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #7 on: August 03, 2019, 03:51:35 pm »
Ah cool I see now :). There's also a 9999 later on. :) Thanks.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculate Pi
« Reply #8 on: August 03, 2019, 04:51:54 pm »
BTW PI is not only irrational it is transcendental "—that is, not a root (i.e., solution) of a nonzero polynomial equation with integer coefficients."
« Last Edit: August 03, 2019, 04:56:01 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #9 on: August 03, 2019, 05:24:30 pm »
I just found the World Record for calculating Pi... don't try it with this program LOL.
A person calculated up to 31 trillion digits! So I wondered to myself how long that would take with this little program, that is if the computer somehow stayed on the whole time without burning up into toast lol. So I made me a little program to calculate... BASIC is so cool with just doing math sometimes lol. I will say the answer after I post my little program. I think I did it right anyway.
Code: QB64: [Select]
  1. DIM minutes AS DOUBLE
  2. DIM hours AS DOUBLE
  3. DIM years AS DOUBLE
  4. 'How many digits of Pi for the record, 31 trillion.
  5. her = 31000000000000
  6. 'How many digita of Pi I made in 39 minutes.
  7. me = 100000
  8.  
  9. 'How many minutes it would take.
  10. z = her / me
  11. minutes = 39 / z
  12.  
  13. hours = minutes / 60
  14. days = hours / 24
  15.  
  16. 'How many years it would take to process 31 trillion digits with my program.
  17. years = days / 365
  18. PRINT years
  19.  

Answer: It would take 15.123 Years! LOL She must have used a supercomputer.


« Last Edit: August 03, 2019, 05:27:29 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Calculate Pi
« Reply #10 on: August 02, 2020, 12:52:34 am »
Here is an updated version of this Pi calculating program. I found this code exactly a year ago and updated it tonight to show how many numbers it has left to calculate. I don't suggest over 20000 digits though. Although I did use it once to get 100000. I also added a _LIMIT 300 command tonight so it doesn't use up so much computer computation.
I remembered this program from Felippe's graphical Pi program he posted tonight. I don't know how perfect this program is to calculate Pi, it's just code I found online a year ago.

Code: QB64: [Select]
  1. 'Almost all of the following code was posted by someone on http://www.codecodex.com/wiki/Calculate_digits_of_pi
  2. 'That was taken from American Mathematical Monthly (see below).
  3. 'I converted it to also make a .txt file of Pi.txt if the user wishes to.
  4.  
  5.  
  6. 'PI.BAS 'Prints PI to number of decimal places desired. 19830623tac.
  7. '        Ref: American Mathematical Monthly vol#45,1938 p657-667.
  8. '        Ref: Best of Micro vol.#1 p85-86.
  9. '
  10. '
  11. '             *       ----                      ----
  12. '        *****         \     16(-1)^(k+1)        \         4(-1)^(k+1)
  13. '       * * *   ---     >   ------------    --    >    ----------------
  14. '         * *   ---    /    (2k-1)5^(2k-1)       /      (2k-1)239^(2k-1)
  15. '         * *         ----                      ----
  16. '        *   *        k=1                       k=1
  17. '
  18. '
  19.  
  20.  
  21. SCREEN _NEWIMAGE(800, 600, 32)
  22. PRINT "                      Calculate Pi"
  23. PRINT "This program will produce Pi to as many digits as you wish."
  24. PRINT "But I do not recommend going over 20000 because it's pretty slow."
  25. PRINT "Do you wish to also have it make a .txt file called Pi.txt"
  26. PRINT "that can be opened with a program such as Windows Notepad?"
  27. PRINT "The program will also open Pi.txt after it is finished making it."
  28. PRINT "If you just see it on the screen, you will only see as much as"
  29. PRINT "the screen can show at the end."
  30. INPUT "Type Yes or No here:"; ff$
  31. IF LEFT$(ff$, 1) = "Y" OR LEFT$(ff$, 1) = "y" THEN file = 1
  32. main:
  33. TEN = 100
  34. INPUT "Enter number of digits wanted"; SIZE:
  35. SIZE = (SIZE + 1) OR 1
  36. PLACES = SIZE - 1
  37.  
  38. IF SIZE > 200 THEN TEN = 10 ELSE SIZE = (SIZE + 1) / 2
  39.  
  40. DIM POWER(SIZE), TERM(SIZE), RESULT(SIZE)
  41. K(1) = 25: K(2) = 239 'Constants
  42. C(1) = 5: C(2) = 239
  43.  
  44. 'Main loop.
  45. FOR PASS = 1 TO 2
  46.     Init:
  47.     FOR L = 0 TO SIZE
  48.         POWER(L) = 0: TERM(L) = 0: IF PASS = 1 THEN RESULT = 0
  49.     NEXT L
  50.     POWER(0) = 16 / PASS ^ 2
  51.     MODE = 0
  52.     DIVISOR = C(PASS)
  53.     GOSUB divide
  54.     XPONENT = 1
  55.     SIGN = 3 - 2 * PASS
  56.     '
  57.  
  58.     copy:
  59.     FOR L = 0 TO SIZE: TERM(L) = POWER(L): NEXT L
  60.     '
  61.     _LIMIT 300
  62.     sz = sz + 1
  63.     LOCATE 3, 1: PRINT SIZE - sz
  64.  
  65.     MODE = 1
  66.     DIVISOR = XPONENT
  67.     GOSUB divide
  68.  
  69.     MODE = (SIGN < 0) + ABS(SIGN > 0)
  70.     GOSUB addsub
  71.  
  72.     XPONENT = XPONENT + 2
  73.     SIGN = -SIGN
  74.  
  75.     MODE = 0
  76.     DIVISOR = K(PASS)
  77.     GOSUB divide
  78.  
  79.     IF PASS = 2 THEN GOSUB divide
  80.  
  81.     IF ZERO <> 0 THEN GOTO copy
  82.  
  83. NEXT PASS
  84. '
  85. 'Print result.
  86. PRINT "The value of PI to"; PLACES; " decimal places"
  87. PRINT RESULT(0); ".";
  88. IF file = 1 THEN OPEN "pi.txt" FOR OUTPUT AS #1
  89. PRINT #1, "3 . ";
  90. FOR L = 1 TO SIZE
  91.     IF (TEN = 100) * (RESULT(L) < 10) THEN
  92.         PRINT "0";
  93.         PRINT USING "#"; RESULT(L);
  94.         IF file = 1 THEN PRINT #1, "0";
  95.         IF file = 1 THEN PRINT #1, USING "#"; RESULT(L);
  96.         GOTO nxt
  97.     END IF
  98.     IF file = 1 THEN IF L / 30 = INT(L / 30) THEN PRINT #1, " "
  99.     PRINT USING "##"; RESULT(L);
  100.     IF file = 1 THEN PRINT #1, USING "##"; RESULT(L);
  101.     nxt:
  102. IF file = 1 THEN CLOSE #1
  103. IF file = 1 THEN SHELL _DONTWAIT "Pi.txt"
  104.  
  105. divide:
  106. 'Division subroutine.   mode0 =  Power(x)/Div else mode1 =  Term(x)/Div.
  107. DIGIT = 0: ZERO = 0
  108. FOR L1 = 0 TO SIZE
  109.     DIGIT = DIGIT + TERM(L1) * MODE + POWER(L1) - POWER(L1) * MODE
  110.     QUOTIENT = INT(DIGIT / DIVISOR)
  111.     RESIDUE = DIGIT MOD DIVISOR
  112.     ZERO = ZERO OR (QUOTIENT + RESIDUE)
  113.     IF MODE THEN TERM(L1) = QUOTIENT ELSE POWER(L1) = QUOTIENT
  114.     DIGIT = RESIDUE * TEN
  115. NEXT L1
  116. MODE = 0
  117.  
  118. addsub:
  119. 'Add/Subtract subroutine.       mode - 1 =  Subtract  else mode1 = Add.
  120. CARRY = 0
  121. FOR L1 = SIZE TO 0 STEP -1
  122.     SUM = RESULT(L1) + TERM(L1) * MODE + CARRY * MODE: CARRY = 0
  123.     IF (MODE = 1) * (SUM < TEN) + (MODE = -1) * (SUM >= 0) THEN GOTO t1
  124.     SUM = SUM + MODE * -TEN: CARRY = 1
  125.  
  126.     t1:
  127.     RESULT(L1) = SUM
  128. NEXT L1
  129. MODE = 0
  130.