Advertisement
jukaukor

Pii_hexadecimals.jl

Feb 6th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # calculates nth hex decimal of Pi
  2. # using Bailey-Borwein-Plouffe
  3. # https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
  4. # implements https://giordano.github.io/blog/2017-11-21-hexadecimal-pi/
  5.  
  6. fpart(x) = mod(x, one(x)) # ottaa luvun x:n desimaalit
  7. # Return the fractional part of x, modulo 1, always positive
  8.  
  9. function Σ(n, j)
  10. # Compute the finite sum, BBP termit Σ
  11. s = 0.0
  12. denom = j
  13. for k in 0:n
  14. s = fpart(s + powermod(16, n - k, denom) / denom)
  15. denom += 8
  16. end
  17. # Compute the infinite sum, jälkimmäinen BBP termi, äärettönään asti
  18. num = 1 / 16
  19. while (frac = num / denom) > eps(s) # poistutaan kun osamäärä alittaa eps-tarkkuuden
  20. s += frac
  21. num /= 16 # joka kierroksella jaetaan 16
  22. denom += 8 # joka kierroksella nim kasvaa 8
  23. end
  24. return fpart(s)
  25. end
  26. # dₙ = 16*[4Σ₁ - 2Σ₂ - Σ₃ - Σ₄] n+1 -th fractional digit, tästä desimaaliosa poistetaan
  27. pi_digit(n) = floor(Int, 16 * fpart(4Σ(n-1, 1) - 2Σ(n-1, 4) - Σ(n-1, 5) - Σ(n-1, 6)))
  28.  
  29. function nth_hex_of_pi(n) # tulostuu Piin n:s hexanumero
  30. print(string(pi_digit(n), base = 16))
  31. end
  32. function PiinHexanumerot(n) # Piin numerot peräkkäin n asti
  33. println("Kaikki Piin hexanumerot peräkkäin")
  34. for i = 0 : n
  35. nth_hex_of_pi(i)
  36. end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement