Standard escape codes are prefixed with Escape
:
- Ctrl-Key:
^[
- Octal:
\033
- Unicode:
\u001b
- Hexadecimal:
\x1B
- Decimal:
27
""" | |
This algorithm is based on an unproven conjecture but successfully produces at least the first 600 thousand digits. | |
Read more about it here: https://www.gavalas.dev/blog/spigot-algorithms-for-pi-in-python/ | |
""" | |
def gospers_tau_unproven(): | |
q,r,t,i = 1, 180, 30, 2 | |
while True: | |
u,y = 3*(3*i+1)*(3*i+2), (q*(27*i-12)+5*r)//(5*t) | |
yield y |
def do(snake: t.Tensor, action: int): | |
positions = snake.flatten().topk(2)[1] | |
[pos_cur, pos_prev] = [T(unravel(x, snake.shape)) for x in positions] | |
rotation = T([[0, -1], [1, 0]]).matrix_power(3 + action) | |
pos_next = (pos_cur + (pos_cur - pos_prev) @ rotation) % T(snake.shape) | |
if (snake[tuple(pos_next)] > 0).any(): | |
return (snake[tuple(pos_cur)] - 2).item() | |
if snake[tuple(pos_next)] == -1: | |
pos_food = (snake == 0).flatten().to(t.float).multinomial(1)[0] |
""" | |
This algorithm is based on an unproven conjecture but successfully produces at least the first 1 million digits. | |
Read more about it here: https://www.gavalas.dev/blog/spigot-algorithms-for-pi-in-python/ | |
""" | |
def gospers_pi_unproven(): | |
q,r,t,i = 1, 180, 60, 2 | |
while True: | |
u,y = 3*(3*i+1)*(3*i+2), (q*(27*i-12)+5*r)//(5*t) | |
yield y |
# requirements: python3, numba, dask | |
import random | |
import numba | |
import dask | |
@dask.delayed | |
@numba.jit(nopython=True, nogil=True) | |
def calc_pi(N): |
def degrees_to_cardinal(d): | |
''' | |
note: this is highly approximate... | |
''' | |
dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", | |
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] | |
ix = int((d + 11.25)/22.5) | |
return dirs[ix % 16] |
""" | |
Run the algorithm below using CPython, Cython, PyPy and Numba and compare | |
their performance. (This is implementing a spigot algorithm by A. Sale, | |
D. Saada, S. Rabinowitz, mentioned on | |
http://mail.python.org/pipermail/edu-sig/2012-December/010721.html). | |
""" | |
def pi_digits(n): | |
"Generate n digits of Pi." | |
k, a, b, a1, b1 = 2, 4, 1, 12, 4 |