Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@igorvanloo
igorvanloo / p98.py
Created July 31, 2021 13:02
Problem 98
def wordchecker(word, number):
word_fingerprint = []
for x in word:
word_fingerprint.append(word.count(x))
number_fingerprint = []
for x in number:
number_fingerprint.append(number.count(x))
if word_fingerprint == number_fingerprint:
@igorvanloo
igorvanloo / p101.py
Created July 31, 2021 17:26
Problem 101
def PolynomialInterpolator(sequence):
if len(sequence) == 1: #Basic case
return sequence[0][1]
elif len(sequence) == 2: #Still basic
return sequence[1][1] + (sequence[1][1] - sequence[0][1])
else: #Using Lagrange's Formula
length = len(sequence)
goal = length + 1
total = 0
for x in range(length):
@igorvanloo
igorvanloo / p107.py
Created July 31, 2021 17:39
Problem 107
def PrimsAlgorithm(graph):
#Find dimension of graph, as well as previous weight
dimension = len(graph)
Previous_Weight = sum([graph[x][y] for x in range(dimension) for y in range(x+1, dimension) if graph[x][y] != 0])
Tree = set([0]) #Step 1
New_Weight = 0
for x in range(dimension - 1):
Minimum_edge, Corresponding_vertex = min([(graph[x][y], y) for x in Tree \
@igorvanloo
igorvanloo / p110.py
Last active August 1, 2021 14:16
Problem 110
def compute(): #Ugliest code known to man
minimum = 614889782588491410
for e1 in range(1,60):
print("e1", e1)
divisors = (2*e1 + 1)
number = 2**e1
for e2 in range(1, 23):
divisors = (2*e1 + 1)*(2*e2 + 1)
number = 2**e1 * 3**e2
if number > minimum:
@igorvanloo
igorvanloo / pe136.py
Last active December 3, 2021 12:47
pe136
def compute(limit): #set limit to 5*10**6
array = [0]*(limit)
for a in range(1, limit):
for d in range(int(math.floor(a/4))+1, a):
n = a*(4*d-a)
if n > limit-1:
break
else:
array[n] += 1
return array.count(1)
@igorvanloo
igorvanloo / p150.py
Created December 6, 2021 10:19
p150
def triangle_generator():
t = 0
s = []
for k in range(1,500501):
t = (615949*t + 797807) % 2**20
s.append(t - 2**19)
#First I generate the s values
triangle = []
r = 1000
while r != 0:
@igorvanloo
igorvanloo / p491.py
Created December 9, 2021 05:56
p491
import itertools
def number_of_doubles(pand):
doubles = 0
for x in range(0,10):
if pand.count(str(x)) == 2:
doubles += 1
return doubles
def compute():
@igorvanloo
igorvanloo / lcm.py
Created December 11, 2021 11:22
LCM Function
def lcm(numbers):
n = sorted(numbers) #I want to start with the largest numbers
curr = n.pop(-1) #Remove the biggest
while len(n) != 0:
temp = n.pop(-1)
curr = int(abs(curr*temp)/math.gcd(curr, temp)) #Continuously find the lcm between the 2 largest numbers
return curr
@igorvanloo
igorvanloo / phi.py
Created December 16, 2021 04:43
Euler Totient Function
def phi(n):
if n == 1:
return 1
phi = 1
d = 2
while n > 1:
count = 0
while n % d == 0:
count += 1
n /= d
@igorvanloo
igorvanloo / mobius.py
Created December 16, 2021 04:45
Möbius function
def Mobius(n):
if n == 1:
return 1
d = 2
num_of_primes = 0
while n > 1:
while n % d == 0:
num_of_primes += 1
if n % (d*d) == 0:
return 0