R

@RealSaitama

Mean Distance Circle

Python
6 months ago
from random import random from math import pi, exp, cos, sin n = 1000000 c = 0 for _ in range(n): x = random() r = abs(1 - complex(cos(2*pi*x), sin(2*pi*x))) c += r f = c / n

Tonelli Shanks

Python
6 months ago
def solve(a, p): r = pow(a, (p - 1) // 2, p) if (r + 1) % p == 0: return None print("ok", r) if p % 3 == 4: return pow(a, (p + 1) // 4, p) b = bin(p - 1)[2:] s = 0 n = len(b)

Des chiffres et des lettres

Python
6 months ago
from itertools import permutations def solve(ns, target): if len(ns) == 1 and target == ns[0]: return str(ns[0]) if len(ns) == 2: x, y = ns[0], ns[1] if x + y == target: return (True, f"({x}+{y})")

Ruler

Python
6 months ago
def ruler(t): m = 2**(t-1) + 1 lst = [''] * m lst[0] = lst[m - 1] = '-' * t def rec(a, b, s): m = (a + b) // 2 lst[m] = '-' * (s - 1) if s > 2: rec(a, m, s - 1)

Lady Bug

Python
6 months ago
from random import randint N = 6 n = 10000 def nxt(x, N): sgn = randint(0, 1) * 2 - 1 y = (x + sgn) % N return y if y != 0 else N

3d walk

Python
6 months ago
import numpy as np from scipy.integrate import quad from scipy.special import ive def G_func(point): x, y, z = np.abs(point) # The integrand is stable using ive (exponentially scaled Bessel) def integrand(t): return ive(x, t/3)

SAT codewars

Python
7 months ago
import sys # Increase recursion depth for large puzzles sys.setrecursionlimit(2000) def solve(cells): n = len(cells) # We map values 1...n to indices 0...n-1 # Graph: Left side (cells 0 to n-1), Right side (values 0 to n-1)

Attribute Counter Access

Python
7 months ago
def track_attributes(cls): class TrackedWrapper: def __init__(self, value, count): self.__dict__['_value'] = value self.count = count def __getattr__(self, name): return getattr(self._value, n

Hyper Sum

Python
7 months ago
M = 1_000_003 def f(n, k): i = pow(pow(n, k, M) - 1 + M, M - 2, M) return (n - pow(n, k, M) - pow(n, (1 + k), M) + pow(n, (k *(1 + n)), M)) * (pow(i, 2, M)) def g(n, k): return (n - n**k -n**(1+k)+n**(k*(1+n))) //(1-n**k)**2 def _solve

Max Clique

Python
8 months ago
from random import randint def ramsey(G): if not G: return set(), set() idx = randint(0, len(G) - 1) nodes = list(G.keys()) node = nodes[idx] neighbors = (nbr for nbr in G[node] if nbr != node)

Astar Algorithm

Python
8 months ago
from heapq import heappush, heappop from itertools import count DIRS = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)) def distance(a, b): return sum(abs(x - y) for x, y in zip(a, b)) def astar_path(G, source, target, h

Pick Until Match (Codewars)

Python
8 months ago
from functools import cache, reduce from math import gcd from fractions import Fraction lcm = lambda xs: reduce(lambda acc, x: acc * x // gcd(acc, x), xs) @cache def dp(tup): m = len(tup) idx = next((i for i in range(m) if tup[i] == 0), No

Networkx

Python
8 months ago
from collections import deque from heapq import heappush, heappop demand, capacity, weight = 'demand', 'capacity', 'weight' def _dijkstra(G, source, get_weight, pred=None, paths=None, cutoff=None, target=None): G_succ = G.succ if G.is_directed(

AL KINDI 2023

Python
8 months ago
d={'A':'P','B':'E','C':'I','D':'G','E':'V','F':'U','G':'K','H':'X','I':'A','J':'S','K':'N','L':'Z','M':'Y','N':'J','O':'M','P':'F','Q':'H','R':'C','S':'B','T':'R','U':'O','V':'T','W':'L','X':'D','Y':'W','Z':'Q'} d={'A':'T', 'B':'K','C':'F','D':'R','

Look and Say

Python
8 months ago
s = "11" cnt = {'1':2} from collections import defaultdict def _next(c): _c = dict(c) for k, v in c.items(): for a in str(v): _c[a] = _c.get(a, 0) + 1 _c[k] = _c.get(k, 0) + 1

Break The pieces

Python
8 months ago
def scale_hrz(s): scaled = [] for i, row in enumerate(s): r = "" for j in range(len(row) - 1): if row[j] == '+' and row[j + 1] == '+': r += '+-' elif row[j] == '+' and row[j + 1

AL KINDI 2024

Python
8 months ago
s = "CRKQDEIVPIMPAIXZNONYTJUSWIKSLUSEAQGFWMAYBOHUUVAMZTVICKYQURVKJHIUTEFHIATEYLBEDXCMWRPOIAYZXALIRGESSSTOLPBEARYPEPHNJTZEYLUXHJHHDIOMQUYUTYXIGNSHOTTEANOCEMFXOBELMWDNUFUGHJINUCPIOHSGJTXONAIPCWWPFDRIXOPMBPOVEWOPYILQLGATOZEUDXXHIUVQUZQPFJ" ds = ('ZERO

JS SAT solver

NodeJS
8 months ago
/* * sat.js * (C) 2012, all rights reserved, * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of th

Median of n sorted arrays

Python
8 months ago
from bisect import bisect_right def kth_element(arrs, k): low = min(arr[0] for arr in arrs if len(arr)) high = max(arr[-1] for arr in arrs if len(arr)) while low < high: mid = low + (high - low) // 2 cnt = 0 for

Between rationals

Python
9 months ago
from math import ceil, floor def between(a, b, c, d): q = 1 while 1: u, v = floor(a * q / b) + 1, ceil(c * q / d) - 1 if u <= v: print(f"{a}/{b} < {v}/{q} < {c}/{d}") return (v, q) q += 1