This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def numberToBase(n, b): | |
if n == 0: | |
return [0] | |
digits = [] | |
while n != 0: | |
digits.append(int(n % b)) | |
n //= b | |
return digits[::-1] |
OlderNewer