def sumof_non_sumofabundant_under(limit): l_p = util.primesfrom2to(limit) l_a = util.abundantto(limit, l_p) l_ax, l_ay = np.meshgrid(l_a, l_a, sparse=True) possible_in_sum = np.unique(l_ax + l_ay) under_limit = possible_in_sum[possible_in_sum < limit] return np.sum(np.arange(limit)) - np.sum(under_limit)
def __init__(self, max_p, a, b): self.primelist = util.primesfrom2to(max_p) print("finished making primes") a_l = np.arange(-a + 1, a) b_tmp = np.arange(0, b + 1) b_mask = np.isin(b_tmp, self.primelist) b_l = b_tmp[b_mask] self.a, self.b = np.meshgrid(a_l, b_l)
def sumoffactor(n, l_p=False): if l_p.any(): l_p = l_p else: l_p = util.primesfrom2to(n) l_f = util.factor(n, l_p) ret = 1 for i in range(len(l_f)): if l_f[i]: ret *= sumto(l_f[i], l_p[i]) return ret - n
def amicablein(n, l_p=False, perfect=False): if l_p.any(): l_p = l_p else: l_p = util.primesfrom2to(n) nums = np.arange(2, n + 1, 1, dtype=int) check = np.ones_like(nums, dtype=bool) ret = np.zeros_like(nums, dtype=bool) for i in range(len(check)): sys.stdout.write("\ri:{0:5d}".format(i)) sys.stdout.flush() if check[i] == True: pair = sumoffactor(nums[i], l_p) if pair <= 10000: tmp = sumoffactor(pair, l_p) if tmp == nums[i] and (perfect or not pair == tmp): sys.stdout.write("\rpair {0:5d}, {1:5d}\n".format( int(pair), int(tmp))) sys.stdout.flush() ret[i] = True ret[int(pair) - 2] = True check[i] = False check[int(pair) - 2] = False return nums[ret]
def amicablein(n, l_p=False, perfect=False): if l_p.any(): l_p = l_p else: l_p = util.primesfrom2to(n) nums = np.arange(2, n + 1, 1, dtype=int) check = np.ones_like(nums, dtype=bool) ret = np.zeros_like(nums, dtype=bool) for i in range(len(check)): sys.stdout.write("\ri:{0:5d}".format(i)) sys.stdout.flush() if check[i] == True: pair = sumoffactor(nums[i], l_p) if pair <= 10000: tmp = sumoffactor(pair, l_p) if tmp == nums[i] and (perfect or not pair == tmp): sys.stdout.write("\rpair {0:5d}, {1:5d}\n".format( int(pair), int(tmp))) sys.stdout.flush() ret[i] = True ret[int(pair) - 2] = True check[i] = False check[int(pair) - 2] = False return nums[ret] if __name__ == '__main__': primes = util.primesfrom2to(10000) print("\rsum of amicables:{}".format(np.sum(amicablein(10000, primes))))
return ret def possible3mult(num): return num % 3 == 0 if __name__ == "__main__": sys.path.append("..") import eulerutil.util as util parser = argparse.ArgumentParser() parser.add_argument("num", type=int) args = parser.parse_args() p_l = util.primesfrom2to(args.num) red_p_l = p_l[np.logical_not(possible3mult(p_l))] mask = np.zeros_like(red_p_l, dtype=bool) for i, p in enumerate(red_p_l): mask[i] = not is2_5mult(p) red_p_l = red_p_l[mask] tried = [] p_circ = [2, 3, 5] for p in red_p_l: if p not in tried: rots = [] power = np.int(np.log10(p)) prv = p for i in range(power+1): prv = prv // 10 + (prv % 10)*10**power