def adjust_params(self, params): """ Adjust the parameters based on its contraints. """ def filter_non_power_of_two(x): if np.log2(x) != int(np.log2(x)): return True return False # Making all factors to be even numbers to have more divisors for p, param in self.design.params_config["tunable"].items(): params[p] = int(np.ceil(params[p] / 2) * 2) # Making all divisor factors to be divisors of the dependent variable for p, param in self.design.params_config["tunable"].items(): #print(param) if "divisors" in param: if "tags" in param and "power_of_two" in param["tags"]: choices = utils.get_divisors(params[param["divisors"][0]], filter_non_power_of_two) else: choices = utils.get_divisors(params[param["divisors"][0]], None) idx = bisect.bisect(choices, params[p]) if idx >= len(choices): idx -= 1 if idx > 1: if abs(choices[idx - 1] - params[p]) < abs(choices[idx] - params[p]): idx -= 1 params[p] = choices[idx] return params
def cleverWay(): ct = time.time() for m in get_divisors(pifSum/2): for k in get_divisors(pifSum/(2*m)): if k - m < m and k - m > 0 and fractions._gcd(m,k - m) == 1: n = k - m d = pifSum/(2*m*k) print('cleverWay', (m**2+n**2)*d, 2*m*n*d, (m**2-n**2)*d, time.time() - ct)
def task21(): results = [] excluded = [] for i in range(1, 100000): if i in excluded: continue divSum = sum(get_divisors(i, True)) if divSum <= 100000: divSum2 = sum(get_divisors(divSum, True)) if divSum2 == i and divSum != i: results.extend((i , divSum)) print(results)
def run(): """ Another pretty straightforward one because I already wrote all the tools necessary """ MAX = 9999 total = 0 for number in xrange(MAX): s = sum(get_divisors(number)) - number s2 = sum(get_divisors(s)) - s if s2 == number and s != number: total += number return total
def dict_sum_divisors(n): all_primes = prime_numbers(n) sum_divisors = {} for num in range(2, (n + 1)): if num not in all_primes: divisors = get_divisors(num, all_primes) sum_divisors[num] = np.sum(divisors) return sum_divisors
def run(): """ Solution: straightforward. Added more util functions to reuse for later. """ divisors = [] n = 1 while len(divisors) <= 500: n += 1 divisors = get_divisors(get_sum_of_first_n(n), False) return get_sum_of_first_n(n)
def task12(maxNum): i = 0 nextLen = [] maxLen = [] while len(nextLen) < maxNum: j = (i + 1) i += j nextLen = get_divisors(i) if len(maxLen) < len(nextLen): maxLen = nextLen print(len(maxLen), i) print('RESULT', i, nextLen)
def main(): # print get_divisors(28) # print zip(iter_triangle_numbers(), range(7))[-1] first = (x for x in iter_triangle_numbers() if length(get_divisors(x)) > 500).next() print get_divisors(first) print first
def pe3(n=600851475143): """ >>> pe3() [71, 839, 1471, 6857] """ return get_divisors(n)
def get_abundant_numbers(limit): """ Returns all abundant numbers less than a limit """ return [number for number in xrange(12, limit) \ if sum(get_divisors(number)) > 2 * number]
""" Problem : Evaluate the sum of all the amicable numbers under 10000. Performance time: ~0.27s """ from utils import get_divisors from timer import timer timer.start() divisor_sums = {} for number in range(10000): divisor_sums[number] = sum(get_divisors(number)) answer = 0 for k, v in divisor_sums.items(): if v < 10000 and k != v and k == divisor_sums[v]: answer += k print(answer) timer.stop()
def is_abundant(number): return sum(get_divisors(number)) > number
# coding: utf8 # The sequence of triangle numbers is generated by adding the natural numbers. # So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: # # 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... # # Let us list the factors of the first seven triangle numbers: # # 1: 1 # 3: 1,3 # 6: 1,2,3,6 # 10: 1,2,5,10 # 15: 1,3,5,15 # 21: 1,3,7,21 # 28: 1,2,4,7,14,28 # # We can see that 28 is the first triangle number to have over five divisors. # # What is the value of the first triangle number to have over five hundred divisors? from utils import get_divisors not_found = True i = 0 known_divisors = {1: [1]} while not_found: i += 1 triangle_nb = sum([j for j in range(i+1)]) known_divisors = get_divisors(triangle_nb, known_divisors) if len(known_divisors[triangle_nb]) > 500: not_found = False print triangle_nb