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 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 |
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 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): |
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 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: |
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
| 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: |
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
| 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) |
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(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: |
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
| 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): |
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 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 |
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_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 |
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 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]): |
NewerOlder