Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
def c(x, k, d):
x1 = [int(x) for x in str(x)] + [0]
x1 = x1[::-1]
Y = (x//pow(10, k)) *pow(10, k - 1)
if d > 0:
if x1[k] > d:
return Y + pow(10, k - 1)
elif x1[k] == d:
return Y + (x % pow(10, k - 1)) + 1
else:
@igorvanloo
igorvanloo / p826.py
Created February 21, 2025 15:20
p826
import random, fractions
def monte_carlo(trials, n):
total = 0
for _ in range(trials):
painted = 0
pts = sorted([random.random() for _ in range(n)])
prev = 1
for i, x in enumerate(pts):
if i == 0:
@igorvanloo
igorvanloo / p169.py
Created February 21, 2025 04:45
p169
from functools import cache
@cache
def a(n):
if n == 0:
return 0
if n == 1:
return 1
if n % 2 == 0:
return a(n//2)
@igorvanloo
igorvanloo / p186.py
Created February 16, 2025 02:56
p186
def compute(p, number = 524487):
S = [0]
#V keeps tracks of which connected component vertex i is in
V = [i for i in range(10**6)]
#These are the connected components of the graph, initially every vertex is in its own connected component
#The head of the connected component is itself
cc = {i:set([i]) for i in range(10**6)}
k = 1
calls = 0
while True:
@igorvanloo
igorvanloo / heegner.py
Created February 3, 2025 06:10
pheegner
import math
import decimal as dc
def is_sq(x):
sq = (x ** (1 / 2))
if round(sq) ** 2 == x:
return True
return False
def compute(N):
@igorvanloo
igorvanloo / p149.py
Created February 2, 2025 03:20
p149
def MaxContiguousSubarray(A):
currMax = 0
currSum = 0
for i in range(len(A)):
currSum = max(currSum + A[i], A[i])
currMax = max(currMax, currSum)
return currMax
def MaxsumSubseq(M):
currMax = 0
@igorvanloo
igorvanloo / p142.py
Created January 29, 2025 09:39
p142
def is_sq(x):
sqrt = (x ** (1 / 2))
if round(sqrt) ** 2 == x:
return True
return False
def compute():
for b in range(1, 1000):
for a in range(b + 2, 1000, 2):
x = (a*a + b*b)//2
def solve(part, j, S, S_set, S_all_sets, P):
if j == len(part):
S_all_sets.append(S_set)
return S_set
for x in P[part[j]]:
if all(S[int(i)] + 1 != 2 for i in str(x)):
SS = [S[k] for k in range(10)]
for i in str(x):
SS[int(i)] += 1
if x == max(S_set + [x]):
@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