Post Reply 
Little explorations with HP calculators (no Prime)
11-05-2017, 04:05 PM (This post was last modified: 11-05-2017 07:19 PM by pier4r.)
Post: #247
RE: Little explorations with HP calculators (no Prime)
I am slowly continuing to explore the "integer numerator ranges that enclose pi" (I post it here and not in another thread because I already mentioned the exploration here).

At the moment I'd like to see how the numerators approaches pi keeping a dynamic TopX of approximations. The program I assembled so far does a lot of list processing and the listExt library of DavidM is super helpful. Especially KSORT is great.

The program has the following idea:

create a list that contains quintuplets
{ D NL NH VL VH }
D: denominator
NL: numerator low (minimum of the range given the denominator)
NH: numerator high (maximum of the range given the denominator)
VL: value NL/D
VH: value NH/D

I do not just add to the result all the quintuplets between firstDenominator (say 1) and maxDenominator (say 10).
Instead I want to add those approximations that make it to the "actual top5", where actual means: considering the quintuplets computed so far. This excluding quintuplets that produce the same best approximation.

For example
14 43 44 3.071... 3.14285...
is considered equal to
7 21 22 3 3.14285...
because the best approximation produced ( 44/14 ) is already in the top 5, since 22/7 was the first instance found for that approximation. (I may later on check if also the second approximation is worth the top5)

The program is the following
Code:

gpIntegerRangeEnclosePiAttempt201711051450
  \<<
    @use the listExt of davidM
    1               "lvFirstDenominator"                  DROP
    10              "lvLastDenominator"                   DROP
    0               "lvCountElements"                     DROP
    { }             "lvCurrentTopApproximations"          DROP
    { }             "lvCurrentTopApproxDist"              DROP
    5               "lvNumberTopApproxSave"               DROP
    0               "lvValueMin"                          DROP
    0               "lvValueMax"                          DROP
    0               "lvApproxInIteration"                 DROP
    0               "lvDistInIteration"                   DROP
    
    
    \->
    lvFirstDenominator
    lvLastDenominator
    lvCountElements
    lvCurrentTopApproximations
    lvCurrentTopApproxDist
    lvNumberTopApproxSave
    lvValueMin
    lvValueMax
    lvApproxInIteration
    lvDistInIteration
    
    
    \<<
      0 lvNumberTopApproxSave NDUPN \->LIST 
      DUP 'lvCurrentTopApproximations' STO
      \pi - ABS 'lvCurrentTopApproxDist' STO
      
    
      lvFirstDenominator lvLastDenominator
      FOR lvDenominator
        lvDenominator @den
        DUP \pi * IP @min num
        DUP 1 + @max num
          @see explanation in other attemps
        OVER lvDenominator / @min num/den 
        DUP 'lvValueMin' STO
        
        OVER lvDenominator / @max num/den
        DUP 'lvValueMax' STO
        
        IF
          @if there is not already the best approximation inside the top list
          lvValueMin lvValueMax 2 \->LIST @make a list of value
          DUP 'lvApproxInIteration' STO
          DUP \pi - ABS @distance from pi in absolute value
          DUP 'lvDistInIteration' STO
          KSORT @sort by distance
          DROP @drop the distance
          HEAD @pick the approx with the closest distance to PI
          lvCurrentTopApproximations SWAP POS 0 ==
            @the best value is not in the list
        THEN
          @see if any of those two values is in the best approximations
          @optimizations come later
          lvCurrentTopApproximations lvApproxInIteration +
          lvCurrentTopApproxDist lvDistInIteration +
          
          @ sort by the absolute distance from PI
          KSORT
          
          @save the distance
          lvNumberTopApproxSave LFRST 'lvCurrentTopApproxDist' STO
          @save the list of the 5 best approx
          lvNumberTopApproxSave LFRST 'lvCurrentTopApproximations' STO
          
          IF
            lvCurrentTopApproximations lvValueMin POS 0 >
            lvCurrentTopApproximations lvValueMax POS 0 >
            OR
            @ if Any approximation made it in the top list
          THEN
            @save the computations
            'lvCountElements' 5 STO+
          ELSE
            @discard
            5 DROPN
          END
        ELSE
          @discard
          5 DROPN
        END
      NEXT
      
      @we have on the stack many values now, let's pack them in a list
      lvCountElements
      \->LIST
      
    \>>
  \>>

The program runs on my hp50g in 646 seconds for the first 1000 denominators.

And uses 10232 seconds for the first 10k denominators. Seeing the fraction slowly closing in pi is satisfying.

The values so far:
Code:

{ 1. 3. 4. 3. 4. 4. 12. 13. 3. 3.25 5. 15. 16. 3. 3.2 6. 18. 19. 3. 3.16666666667 
7. 21. 22. 3. 3.14285714286 8. 25. 26. 3.125 3.25 
9. 28. 29. 3.11111111111 3.22222222222 10. 31. 32. 3.1 3.2 
11. 34. 35. 3.09090909091 3.18181818182 
13. 40. 41. 3.07692307692 3.15384615385 
15. 47. 48. 3.13333333333 3.2 17. 53. 54. 3.11764705882 3.17647058824 
19. 59. 60. 3.10526315789 3.15789473684 20. 62. 63. 3.1 3.15 
22. 69. 70. 3.13636363636 3.18181818182 23. 72. 73. 3.13043478261 3.17391304348 
27. 84. 85. 3.11111111111 3.14814814815 29. 91. 92. 3.13793103448 3.1724137931 
34. 106. 107. 3.11764705882 3.14705882353 36. 113. 114. 3.13888888889 3.16666666667 
41. 128. 129. 3.12195121951 3.14634146341 43. 135. 136. 3.13953488372 3.16279069767 
48. 150. 151. 3.125 3.14583333333 50. 157. 158. 3.14 3.16 
57. 179. 180. 3.14035087719 3.15789473684 64. 201. 202. 3.140625 3.15625 
71. 223. 224. 3.14084507042 3.15492957746 78. 245. 246. 3.14102564103 3.15384615385 
85. 267. 268. 3.14117647059 3.15294117647 92. 289. 290. 3.14130434783 3.15217391304 
99. 311. 312. 3.14141414141 3.15151515152 106. 333. 334. 3.14150943396 3.15094339623 
113. 354. 355. 3.13274336283 3.14159292035 120. 376. 377. 3.13333333333 3.14166666667 
127. 398. 399. 3.13385826772 3.14173228346 205. 644. 645. 3.14146341463 3.14634146341 
219. 688. 689. 3.14155251142 3.14611872146 233. 731. 732. 3.13733905579 3.14163090129 
325. 1021. 1022. 3.14153846154 3.14461538462 332. 1043. 1044. 3.14156626506 3.14457831325 
346. 1086. 1087. 3.1387283237 3.14161849711 445. 1398. 1399. 3.14157303371 3.14382022472 
459. 1441. 1442. 3.1394335512 3.14161220044 558. 1753. 1754. 3.14157706093 3.14336917563 
572. 1796. 1797. 3.13986013986 3.14160839161 671. 2108. 2109. 3.14157973174 3.14307004471 
685. 2151. 2152. 3.1401459854 3.14160583942 784. 2463. 2464. 3.14158163265 3.14285714286 
798. 2506. 2507. 3.14035087719 3.14160401003 897. 2818. 2819. 3.14158305463 3.14269788183 
911. 2861. 2862. 3.14050493963 3.14160263447 1010. 3173. 3174. 3.14158415842 3.14257425743 
1024. 3216. 3217. 3.140625 3.1416015625 1123. 3528. 3529. 3.14158504007 3.14247551202 
1137. 3571. 3572. 3.14072119613 3.14160070361 1236. 3883. 3884. 3.14158576052 3.14239482201 
1250. 3926. 3927. 3.1408 3.1416 1349. 4238. 4239. 3.14158636027 3.14232765011 
1363. 4281. 4282. 3.14086573734 3.14159941306 1462. 4593. 4594. 3.14158686731 3.14227086183 
1476. 4636. 4637. 3.14092140921 3.14159891599 1575. 4948. 4949. 3.14158730159 3.14222222222 
1589. 4991. 4992. 3.140969163 3.14159848962 1688. 5303. 5304. 3.14158767773 3.14218009479 
1702. 5346. 5347. 3.14101057579 3.14159811986 1801. 5658. 5659. 3.14158800666 3.14214325375 
1815. 5701. 5702. 3.14104683196 3.14159779614 1914. 6013. 6014. 3.14158829676 3.1421107628 
1928. 6056. 6057. 3.14107883817 3.14159751037 2027. 6368. 6369. 3.14158855451 3.14208189443 
2041. 6411. 6412. 3.14110730034 3.14159725625 2140. 6723. 6724. 3.14158878505 3.14205607477 
2154. 6766. 6767. 3.14113277623 3.14159702878 2253. 7078. 7079. 3.14158899245 3.1420328451 
2267. 7121. 7122. 3.1411557124 3.141596824 2366. 7433. 7434. 3.14158918005 3.14201183432 
2380. 7476. 7477. 3.14117647059 3.14159663866 2479. 7788. 7789. 3.14158935054 3.14199273901 
2493. 7831. 7832. 3.14119534697 3.14159647012 2592. 8143. 8144. 3.14158950617 3.14197530864 
2705. 8498. 8499. 3.1415896488 3.14195933457 2818. 8853. 8854. 3.14158977999 3.14194464159 
2931. 9208. 9209. 3.14158990106 3.14193108154 3044. 9563. 9564. 3.14159001314 3.14191852825 
3157. 9918. 9919. 3.1415901172 3.14190687361 3270. 10273. 10274. 3.14159021407 3.14189602446 
3383. 10628. 10629. 3.14159030446 3.14188590009 3496. 10983. 10984. 3.14159038902 3.14187643021 
3609. 11338. 11339. 3.14159046827 3.14186755334 3722. 11693. 11694. 3.14159054272 3.14185921548 
3835. 12048. 12049. 3.14159061278 3.14185136897 3948. 12403. 12404. 3.14159067882 3.14184397163 
4061. 12758. 12759. 3.1415907412 3.14183698596 4174. 13113. 13114. 3.14159080019 3.14183037853 
4287. 13468. 13469. 3.14159085608 3.14182411943 4400. 13823. 13824. 3.14159090909 3.14181818182 
4513. 14178. 14179. 3.14159095945 3.14181254155 4626. 14533. 14534. 3.14159100735 3.14180717683 
4739. 14888. 14889. 3.14159105296 3.14180206795 4852. 15243. 15244. 3.14159109646 3.14179719703 
4965. 15598. 15599. 3.14159113797 3.14179254783 5078. 15953. 15954. 3.14159117763 3.14178810555 
5191. 16308. 16309. 3.14159121557 3.14178385668 5304. 16663. 16664. 3.14159125189 3.14177978884 
5417. 17018. 17019. 3.14159128669 3.14177589071 5530. 17373. 17374. 3.14159132007 3.1417721519 
5643. 17728. 17729. 3.14159135212 3.14176856282 5756. 18083. 18084. 3.1415913829 3.14176511466 
5869. 18438. 18439. 3.14159141251 3.14176179928 5982. 18793. 18794. 3.14159144099 3.14175860916 
6095. 19148. 19149. 3.14159146842 3.14175553733 6208. 19503. 19504. 3.14159149485 3.14175257732 
6321. 19858. 19859. 3.14159152033 3.14174972315 6434. 20213. 20214. 3.14159154492 3.14174696923 
6547. 20568. 20569. 3.14159156866 3.14174431037 6660. 20923. 20924. 3.14159159159 3.14174174174 
6773. 21278. 21279. 3.14159161376 3.14173925882 6886. 21633. 21634. 3.1415916352 3.14173685739 
6999. 21988. 21989. 3.14159165595 3.1417345335 7112. 22343. 22344. 3.14159167604 3.14173228346 
7225. 22698. 22699. 3.1415916955 3.14173010381 7338. 23053. 23054. 3.14159171436 3.14172799128 
7451. 23408. 23409. 3.14159173265 3.14172594283 7564. 23763. 23764. 3.1415917504 3.14172395558 
7677. 24118. 24119. 3.14159176762 3.14172202683 7790. 24473. 24474. 3.14159178434 3.14172015404 
7903. 24828. 24829. 3.14159180058 3.14171833481 8016. 25183. 25184. 3.14159181637 3.14171656687 
8129. 25538. 25539. 3.14159183171 3.14171484807 8242. 25893. 25894. 3.14159184664 3.14171317641 
8355. 26248. 26249. 3.14159186116 3.14171154997 8468. 26603. 26604. 3.1415918753 3.14170996693 
8581. 26958. 26959. 3.14159188906 3.14170842559 8694. 27313. 27314. 3.14159190246 3.14170692432 
8807. 27668. 27669. 3.14159191552 3.14170546156 8920. 28023. 28024. 3.14159192825 3.14170403587 
9033. 28378. 28379. 3.14159194066 3.14170264585 9146. 28733. 28734. 3.14159195277 3.14170129018 
9259. 29088. 29089. 3.14159196458 3.1416999676 9372. 29443. 29444. 3.1415919761 3.14169867691 
9485. 29798. 29799. 3.14159198735 3.14169741697 9598. 30153. 30154. 3.14159199833 3.14169618671 
9711. 30508. 30509. 3.14159200906 3.14169498507 9824. 30863. 30864. 3.14159201954 3.14169381107 
9937. 31218. 31219. 3.14159202979 3.14169266378 }

I do not use heavy stack manipulations, although I use them more than before. For example I build the list only at the end of the iterations, as suggested by John Keith. I avoid too much stack acrobatics due to readability of the code but I am pretty sure other people could make the program way faster.

Therefore I posted it because I am curious to see how it can be optimized.

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Little explorations with HP calculators (no Prime) - pier4r - 11-05-2017 04:05 PM



User(s) browsing this thread: 2 Guest(s)