Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@igorvanloo
igorvanloo / p122.py
Created January 14, 2024 15:40
p122 - Efficient Exponentiation
def compute(limit):
v = [0]*(limit + 1) #v[k] = minimum number of multiplication to get n^k
count = 1 #Counting the number of solved k's. It's used to stop the while loop
stack = [(1,)] #The start stack, the only path of length 0 in the tree
currdepth = 1 #Keeping track of the current depth we are in the tree
while count != limit:
#We continue till we have found all 0 < k < 201
@igorvanloo
igorvanloo / p103_is_special_sum_set.py
Created December 29, 2023 08:03
p103_is_special_sum_set
def is_special_sum_set(A):
l = len(A)
#Generate all subsets and categorize them by length of set
#Then subsets[x] = all subsets of length x
subsets = [[] for x in range(l + 1)]
#Keep track of sums to test for duplicates (Condition 1)
sums = []
#Here we generate all subsets while checking if condition 1 is satisfied
@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