Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@igorvanloo
igorvanloo / p18.py
Created July 21, 2021 05:10
Problem 18
def top_to_bottom(tri):
for i in range(1,len(tri)):
a = len(triangle[i])
for j in range(0, a):
if j == 0:
tri[i][j] += tri[i-1][j]
elif j == a-1:
tri[i][j] += tri[i-1][j-1]
else:
tri[i][j] += max(tri[i-1][j],tri[i-1][j-1])
@igorvanloo
igorvanloo / p17.py
Created July 21, 2021 06:01
Problem 17
def Number_to_english(x):
if x < 20:
return ONES[x]
elif 20 <= x < 100:
return TENS[x // 10] + ONES[x % 10]
elif 100 <= x < 1000:
if x == 100:
return "onehundred"
else:
if x % 100 == 0:
@igorvanloo
igorvanloo / p206.py
Created July 21, 2021 06:28
Problem 206
def compute():
start = 101010100
end = 1389026620
while start < end:
if str((start+30)**2)[::2] == "1234567890":
start += 30
break
if str((start+70)**2)[::2] == "1234567890":
start += 70
break
@igorvanloo
igorvanloo / p81.py
Last active July 21, 2021 06:37
Problem 81
def compute():
matrix = [[131, 673, 234, 103,18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331]]
y = len(matrix)
x = len(matrix[0])
for i in (range(x)):
for j in (range(y)):
@igorvanloo
igorvanloo / p76.py
Last active July 21, 2021 06:35
Problem 76
def Partition(goal, alist):
ways = [1] + [0] * (goal)
for options in alist:
for i in range(len(ways) - options):
ways[i + options] += ways[i]
return ways[-1]-1
'''
Sample Output for partitioning 5:
@igorvanloo
igorvanloo / PrimeGenFunc.py
Last active July 21, 2021 09:04
Prime Generator Function
#Returns a list of True and False indicating if a number is prime, for example list[7] = True, list[8] = False
def list_primality(n):
result = [True] * (n + 1)
result[0] = result[1] = False
for i in range(int(math.sqrt(n)) + 1):
if result[i]:
for j in range(2 * i, len(result), i):
result[j] = False
return result
@igorvanloo
igorvanloo / primefacfunc.py
Created July 21, 2021 13:31
Prime Factors Function
def prime_factors(n):
factors = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n /= d
d = d + 1
if d*d > n:
if n > 1:
@igorvanloo
igorvanloo / isprime.py
Created July 21, 2021 13:33
Is prime Function
def is_prime(x):
if x <= 1:
return False
elif x <= 3:
return True
elif x % 2 == 0:
return False
else:
for i in range(3, int(math.sqrt(x)) + 1, 2):
if x % i == 0:
@igorvanloo
igorvanloo / fibgen.py
Created July 21, 2021 13:35
Fibonacci Generator
def fibonnaci(n): #Finds the nth fibonnaci number
v1, v2, v3 = 1, 1, 0 # initialise a matrix [[1,1],[1,0]]
for rec in bin(n)[3:]: # perform fast exponentiation of the matrix (quickly raise it to the nth power)
calc = v2*v2
v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
if rec=='1':
v1, v2, v3 = v1+v2, v1, v2
return v2
@igorvanloo
igorvanloo / basechanger.py
Created July 21, 2021 13:36
Base Changing Function
def numberToBase(n, b):
if n == 0:
return [0]
digits = []
while n != 0:
digits.append(int(n % b))
n //= b
return digits[::-1]