Skip to content

Instantly share code, notes, and snippets.

View Nikorasu's full-sized avatar
:atom:
..coding..

Nik Nikorasu

:atom:
..coding..
View GitHub Profile
@deeplook
deeplook / pi_digits.py
Created February 13, 2013 20:16
Generate digits of Pi using a spigot algorithm.
"""
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
@RobertSudwarts
RobertSudwarts / deg_to_cardinal.py
Created June 7, 2015 16:18
[python] degrees to cardinal directions
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]
@fnky
fnky / ANSI.md
Last active July 27, 2024 21:04
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@jhidding
jhidding / pi.py
Created January 16, 2020 11:54
Compute Pi using Numba and Dask
# requirements: python3, numba, dask
import random
import numba
import dask
@dask.delayed
@numba.jit(nopython=True, nogil=True)
def calc_pi(N):
@GavalasDev
GavalasDev / piG3.py
Created May 9, 2021 09:03
A faster spigot algorithm for the digits of Pi.
"""
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
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]
@GavalasDev
GavalasDev / tauG3.py
Created August 24, 2022 11:42
A fast spigot algorithm for the digits of Tau.
"""
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