Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@igorvanloo
igorvanloo / p251_square_divisors.py
Created November 21, 2023 15:28
p251 get square divisors
def gen_multiples(primes, multiplicities, limit):
if len(primes) == 0:
return []
p = 1
multiples = []
for i in range(multiplicities[0] + 1):
if p > 1:
multiples.append(p)
if len(primes) > 1:
@igorvanloo
igorvanloo / p229.py
Created November 21, 2023 06:31
p229
def segment(limit):
segmentstart = 0
segmentsize = limit//1000 #adjust according to limit
b7max = [0]*(int(math.sqrt(limit)) + 1) #Keeps track of maximum b given a for each segment
b3max = [0]*(int(math.sqrt(limit)) + 1)
b2max = [0]*(int(math.sqrt(limit)) + 1)
b1max = [0]*(int(math.sqrt(limit)) + 1)
@igorvanloo
igorvanloo / p853_multiples_generator.py
Last active November 18, 2023 04:18
p853 multiples generator
def gen_multiples(primes, pivalues, multiplicities, limit):
#Generates all multiples of given primes up to a cetain multiplicity
#Extra condition to precalculate pi(n)
p, vpi = 1, 1
multiples = []
for i in range(multiplicities[0] + 1):
if p > 1:
multiples.append((p, vpi))
if len(primes) > 1:
@igorvanloo
igorvanloo / p313_SlidingGame.py
Last active June 12, 2023 08:13
p313_SlidingGame
class SlidingGame:
def __init__(self, m, n):
self.board = self.create_board(m, n)
self.solution = self.find_best_path(self.board, m, n)
def create_board(self, m, n):
board = []
for x in range(n):
board.append([1]*m)
def f(m, p):
line1 = '<p>Let $A$ be an <b>affine plane</b> over a <b>radically integral local field</b> $F$ with residual characteristic $p$.</p>'
line2 = '<p>We consider an <b>open oriented line section</b> $U$ of $A$ with normalized Haar measure $m$.</p>'
line3 = '<p>Define $f(m, p)$ as the maximal possible discriminant of the <b>jacobian</b> associated to the <b>orthogonal kernel embedding</b> of $U$ <span style="white-space:nowrap;">into $A$.</span></p>'
line4 = '<p>Find $f(20230401, 57)$. Give as your answer the concatenation of the first letters of each bolded word.</p>'
text = line1 + line2 + line3 + line4
ans = ""
start = 0
@igorvanloo
igorvanloo / p425.2 Widest Path Problem.py
Last active May 25, 2023 06:13
p425.2 Widest Path Problem
import heapq
def widest(graph, start = 0, INFINITY = 10**10):
width = {}
cloud = {}
path = {}
for x in graph:
width[x] = INFINITY
cloud[x] = False
path[x] = (2,) #Optional, returns the actual path, useful for debugging
@igorvanloo
igorvanloo / p425.1 Generate Graph Algo.py
Last active May 25, 2023 05:59
p425.1 Generate Graph Algo
is_prime = #A list such that is_prime[x] = True if x is prime
primes = #A list containing all the primes. Hint: generate it from is_prime
graph = {}
for k in range(len(primes)):
p = primes[k]
digits = []
while p != 0:
digits.append(p % 10)
def genDuoDigits(d, x, y):
#This function generates all duodigits containing the numbers x and y up to d digits
duodigits = set()
combs = {0}
#We start with only the number 0
for i in range(d):
temp = []
#We will now generate all i + 1 digit duodigits
for v in combs:
#For every existing i digit duodigit we have, we can add x or y to the end of it to create an i + 1 digit duodigit
def xorProduct(x, y):
prod = 0
#Note that 0 ^ x = x, ^ = XOR
while y != 0:
if y % 2 == 1:
#If the first bit of y is a 1, prod = prod ^ x
prod ^= x
#Then we shift x one bit to the left, essentially adds a 0 behind x
x <<= 1
#And we push y one bit to the right, essentially removes the first bit
import itertools
from functools import lru_cache
@lru_cache(maxsize = 10**5)
def recursiveGenerate(A):
values = set()
#If our subset has a single element, then that element is the only constructable number
if len(A) == 1:
values.add(A[0])