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