R

@RealSaitama

Optimal Transportation

Python
1 year ago
import numpy as np np.set_printoptions(suppress=True) alpha = 0.99995 eps = 0.00001 beta = 0.1 def make_mat(x, sigma, a): n, m = len(a), len(a[0]) mat = np.array(a)

Interior point method

Python
1 year ago
import numpy as np np.set_printoptions(suppress=True) c = np.array([2, 1.5, 0, 0, 0, 0, 0]) a = np.array([ [12., 24., -1., 0., 0., 0., 0.], [16., 16., 0., -1., 0., 0., 0.], [30., 12., 0., 0., -1., 0., 0.], [1., 0., 0., 0., 0., 1., 0.], [0., 1., 0., 0., 0., 0., 1.]

Ceiling powers

Python
1 year ago
import numpy as np a = 2 b = 3 vs = [2, 2 * a] for i in range(2, 10): vs.append(2 * a * vs[i - 1] + (b - a**2) * vs[i - 2]) print(vs) # def mod_pow(base, exponent, modulus):

Rubikube

Python
1 year ago
VAL = 1.5 cols = "ybrgow" faces = "ulfrbd" pos = { "f0": (1.5, -1, 1), "f1": (1.5, 0, 1), "f2": (1.5, 1, 1), "f3": (1.5, -1, 0), "f4": (1.5, 0, 0),

Maximum visible area of cylinder

Python
1 year ago
xs = [[1, 80], [3, 100], [2, 80], [3, 90], [2, 80]] def select(ls): ans = [] ys = sorted(xs, key=lambda x:(-x[0], -x[1])) print(ys) R = -1 for r, a in ys: if r == R: continue ans.append((r, a))

Short Pascal

Python
1 year ago
f=lambda n:'\n'.join(' '.join(map(lambda x:str(x).rjust(2,' '),(__import__("math").comb(i,k)for k in range(i+1))))for i in range(n+1)) print(f(7))

Hermite Prime Factorization

Python
1 year ago
from collections import Counter from math import prod primes = (3, 5, 7, 11, 13, 17, 19, 23, 29, 31) def factor(m): n = m f = [] for d in [2, 3, 5]: while n % d == 0:

Binom Newton Formula

Python
1 year ago
n = 10 binom = [[1]] for i in range(1, n): r = [1] for j in range(1, i): r.append(binom[i - 1][j] + binom[i - 1][j - 1]) r.append(1) binom.append(r[:]) for line in binom: print(line)

Nonogram

Python
1 year ago
import numpy as np import itertools as it import re from typing import List, Dict, Tuple, Set, AnyStr, Type solution_list: List[Type[np.ndarray]] = [] placements: Dict[str, Dict[int, List[Type[np.ndarray]]]] = {} def compute_blocks(line_len: int, blocks: Tuple):

Sparse Matrices

Python
1 year ago
# https://github.com/gilbertgede/PyIntropt/blob/master/pyintropt/nitro/solver.py import math from typing import Dict, Tuple, Optional, List class SparseMatrix: """Sparse Matrix class Args: data: A dictionary of (i, j) -> value """

Linear Programmation

Python
1 year ago
import numpy as np from scipy.optimize import linprog # https://github.com/scipy/scipy/blob/main/scipy/optimize/_linprog_ip.py def minimum_transportation_price(suppliers, consumers, costs): costs = np.array(costs) costs_flattened = costs.flatten() # Number of suppliers and consumers

Binary strings less than

Python
1 year ago
# https://www.quora.com/How-many-numbers-with-set-bit-count-equal-to-k-are-there-between-any-given-positive-numbers-a-and-b from functools import cache from math import comb @cache def T(n, k): if n < k or k < 0: return 0 return 2**(n+1) - 1 if k == 0 else T(n - 1, k) + T(n - 1, k - 1) + comb(n, k) * 2**n

Maya

Python
1 year ago
xs = [1, 20, 20 * 18, 20 * 18 * 20, 20 * 18 * 20 * 20, 20 * 18 * 20 * 20] def maya(n): if n == 0: return [0] i = 0 while xs[i + 1] <= n: i += 1 ans = [] while n > 0: q, r = divmod(n, xs[i])

Aitken

Python
1 year ago
xs = [[1]] for n in range(1, 15): rs = [xs[n - 1][-1]] for i in range(n): rs.append(rs[i] + xs[n - 1][i]) xs.append(rs) print(xs)

Vecteurs

Python
1 year ago
class Vecteur: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __str__(self): return f"Vecteur({self.x}, {self.y}, {self.z})" def __eq__(self, other):

Substrings Divisible by Power of 2

Python
1 year ago
def count_divisible_substrings(s, p): n = len(s) result = [0] * n mod = 2 ** p # Calculate 2^p for divisibility checks for i in range(n): current_mod = 0 # Iterate through substrings starting at index i for j in range(i, n): current_mod = (current_mod * 10 + int(s[j])) % mod

Continued Fraction

Python
1 year ago
from fractions import Fraction def to_frac(x): [u, v] = [int(x), x % 1] ans = [] i = 0 while v > 0 and i < 10 and u < 1000 : ans.append(u) r = 1 / v u, v = int(r), r % 1

Segment intersection

Python
1 year ago
import math EPS = 1E-9 class Pt: def __init__(self, x, y): self.x = x self.y = y def __lt__(self, other):

Waring's Problem

Python
1 year ago
from functools import cache @cache def solve(n, k): if n <= 0: return [[]] elif n == 1: return [[1]] m = 1 ans = []

Digits counter

Python
1 year ago
def solve(n, k): q, x, ans = n, 1, 0 while q > 0: digit = q % 10 q //= 10 ans += q * x if digit == k: ans += n % x + 1 elif digit > k: ans += x