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
| class SegmentTree: | |
| def __init__(self, n, a, combine): | |
| self.n = n | |
| self.tree = [0] * 2 * self.n | |
| self.op = combine | |
| for i in range(n): | |
| self.tree[n + i] = a[i] | |
| for i in range(self.n - 1, 0, -1): | |
| self.tree[i] = self.op(self.tree[i << 1], self.tree[i << 1 | 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 get_primes_to(n): | |
| D, q = {}, 2 | |
| while q <= n: | |
| if q not in D: | |
| yield q | |
| D[q * q] = [q] | |
| else: | |
| for p in D[q]: | |
| D.setdefault(p + q, []).append(p) | |
| del D[q] |
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 | |
| def gcd(a, b): | |
| a = abs(a) | |
| b = abs(b) | |
| while a: | |
| a, b = b % a, a | |
| return b |
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 get_primes_under(n): | |
| primes = list() | |
| sieve = [0] * (n + 1) | |
| for i in xrange(2, n): | |
| if not sieve[i]: | |
| primes.append(i) | |
| for j in xrange(i + i, n, i): | |
| sieve[j] = 1 | |
| return primes |
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_square(integer): | |
| root = math.sqrt(integer) | |
| if int(root + 0.5) ** 2 == integer: | |
| return True | |
| else: | |
| return False |
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 get_prime_factors(n): | |
| dd = defaultdict(lambda: 0) | |
| if n > 1: | |
| i = 2 | |
| while i * i <= n: | |
| if n % i == 0: | |
| dd[i] += 1 | |
| n /= i | |
| i = 2 | |
| 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
| def get_cycle_len(num): | |
| found_remainders = [0] * num | |
| value = 1 | |
| position = 0 | |
| while not found_remainders[value] and value: | |
| found_remainders[value] = position | |
| value *= 10 | |
| value %= num | |
| position += 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
| private int sumOfFactorsPrime(int number, int[] primelist) { | |
| int n = number; | |
| int sum = 1; | |
| int p = primelist[0]; | |
| int j; | |
| int i = 0; | |
| while (p * p <= n && n > 1 && i < primelist.Length) { | |
| p = primelist[i]; | |
| i++; |
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 base_n(num, base, numerals="0123456789abcdefghijklmnopqrstuvwxyz"): | |
| if not num: | |
| return numerals[0] | |
| res = "" | |
| while num: | |
| res = numerals[num % base] + res | |
| num //= base | |
| return res |
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 | |
| def rotate(n): | |
| digs = int(math.log10(n)) | |
| return (10 ** digs) * (n % 10) + n // 10 |
NewerOlder