R

@RealSaitama

Cantor's enumeration

Python
1 year ago
from math import gcd from itertools import chain, count, islice def gen(): # generator of terms return chain.from_iterable((i, n-i) for n in count(2) for i in range(1, n) if gcd(i, n-i)==1) lst = list(islice(gen(), 10**7)) print(lst[-2], lst[-1])

Archimedes

Python
1 year ago
RAC3 = 3**0.5 s, t = 3 * RAC3 / 2, 3 * RAC3 for _ in range(10): t = (2 * s * t) / (s + t) s = (s * t)**0.5 print(s, t)

Hill Cipher

Python
1 year ago
mat = [[11, 3], [7, 4]] inv_mat = [[16, 1], [11, 5]] abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def pos(l): return abc.index(l) def letter(x): return abc[x % 26]

Sqrt Algorithm

Python
1 year ago
from math import isqrt def sqrt(n): d = len(str(isqrt(n))) a, b = 5 * n, 5 step = 0 while step < 10000: if a >= b: a, b = a - b, b + 10 else:

Latin squares

Python
1 year ago
n = int(input()) sq = [list(map(int, input())) for _ in range(n)] def is_possible(lst): for i in range(n): row = [lst[i][j] for j in range(n) if lst[i][j] > 0 ] if any(not e in range(1, n + 1) for e in row) or any(row.count(e) != 1 for e in row): return False for j in range(n): col = [lst[i][j] for i in range(n) if lst[i][j] > 0] if any(not e in range(1, n + 1) for e in col) or any(col.count(e) != 1 for e in col):

Multiply Puzzle

Python
1 year ago
import re from itertools import product n = int(input()) inp = [input() for _ in range(n)] w = max(len(e) for e in inp) s = '\n'.join(inp) fill = lambda st: list(map(lambda x: int(x) if x.isdigit() else None, re.sub(r'\s+', '', st)))

Insurance

Python
1 year ago
n = 104 s0 = 100 u = 1.02 M = 10**5 from random import random def sim(): vs, k = 0, 0 for _ in range(M): s = s0 pr = 1

Digitwise Addition

Python
1 year ago
from collections import Counter M = 10**9 + 7 dct = {k: 0 for k in range(10)} dct[0] = 1 ans = [(0, 1)] for _ in range(10**5): x = max(k for k in dct if dct[k] > 0) new_dct = dict(dct) for k, v in dct.items():

Luhn

Python
1 year ago
def validate(n): def double(x): return (2 * x) % 10 + (1 if x >= 5 else 0) m = map(int, str(n)[::-1]) d = [double(x) if i % 2 == 1 else x for i, x in enumerate(m)] return sum(d) % 10 == 0

ISSN

Python
1 year ago
def get_key(st): s = st.replace('-', '') x = sum((10-i-2) * int(s[i]) for i in range(7)) r = -x%11 last = str(r) if r != 10 else "X" return last def is_valid(st): key = get_key(st) return key == st[-1]

All nines

Python
1 year ago
def find(n): if n % 2 == 0: return None k = 1 while k < 10000: r = pow(10, k, n) if r == 1: return (10**k - 1) // n k += 1

Polygon Area

Python
1 year ago
points = [[3,-1], [4, 2], [2, 1], [1, 3], [-1, 1], [-3, 2], [-2, -2]] def area(pts): n = len(pts) s = 0 for i in range(n): a = pts[i][0] * pts[(i + 1) % n][1] - pts[i][1] * pts[(i + 1) % n][0] print(a) s += a return s / 2

Markov Ant (Codingame)

Python
1 year ago
''' https://en.wikipedia.org/wiki/Absorbing_Markov_chain https://github.com/daxida/CodinGame-contributions/blob/main/markov-ants/Tran_presentation.pdf (FRENCH) https://idpoisson.fr/berglund/probamass_html/node18.html ''' from fractions import Fraction import numpy as np np.set_printoptions(precision=20)

Markov Ant

Python
1 year ago
N = 5*10**4 n, m = 7, 7 x0, y0 = 3, 3 max_steps = 100 v = 1 from collections import defaultdict from random import randint dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] hsh = defaultdict(int) for _ in range(N):

Mirrors

Python
2 years ago
n = 2 arr = [0.4] * n def solve(n, arr): rs = [0.] rs.extend(arr) rs.append(0.) xs = [[0., 1.]] xs.extend([[0., 0.] for _ in range(n + 1)]) ans = 0

Strobogrammatic

Python
2 years ago
from itertools import count, islice, product def ud(s): return s[::-1].translate({ord('6'):ord('9'), ord('9'):ord('6')}) def agen(): yield from [0, 1, 8] for d in count(2): for start in "1689": for rest in product("01689", repeat=d//2-1): left = start + "".join(rest) right = ud(left) for mid in [[""], ["0", "1", "8"]][d%2]:

Sand Cellular Automata

Python
2 years ago
def _next(b): k = len(b) nb = [[b[i][j] for j in range(k)] for i in range(k)] for i in range(k): for j in range(k): s = 0 for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)): x, y = i + di, j + dj if 0 <= x < k and 0 <= y < k and b[x][y] > 3: