Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
def delta(x):
if x == 0:
return 1
return 0
def Ulam(v, t):
# See S. R. Finch, Patterns in 1-additive sequences, Experimental Mathematics 1 (1992), 57-63.
a = 2
e2 = 2*v + 2
#Finding the starting part of the sequence
def find_next(curr):
#Helper function to find cycles
t = bin(curr)[2:][::-1]
b = [int(x) for x in t + '0'*(6 - len(t))]
nb = b[1:] + [b[0] ^ (b[1] & b[2])]
snb = ''.join([str(x) for x in nb])
curr = int(snb[::-1], 2)
return curr
def lucas(n):
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]):