def UNDEEDED_simplify_fraction(numerator, denominator): num_factors = util.factors(numerator) denom_factors = util.factors(denominator) common_factors = set.intersection(num_factors, denom_factors) common_factors.remove(1) if common_factors: divisor = max(common_factors) # print(f"found common factor {divisor}") numerator, denominator = UNDEEDED_simplify_fraction( numerator / divisor, denominator / divisor) # print(f"done! returned {numerator} / {denominator}") return numerator, denominator
def is_perfect(num): return sum(factors(num) - {num}) == num
def ex7(num): print(len(factors(num)) == 2)
def ex6(num): print(factors(num))
def ex5(): print(factors(120))
def factorSum(x): return (sum(util.factors(x)) - x)
""" Project Euler 12: Find the value of the first triangle number to have over five hundred divisors The sequence of triangle numbers is generated by adding the natural numbers. So the 7th 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: """ import util n = 1 while len(util.factors(util.trianglar(n))) <= 500: n += 1 print n, util.trianglar(n)
import util as ut import math """ https://projecteuler.net/problem=627 Counting products Consider the set S of all possible products of n positive integers not exceeding m, that is S={x1x2…xn|1≤x1,x2,...,xn≤m}. Let F(m,n) be the number of the distinct elements of the set S. For example, F(9,2)=36 and F(30,2)=308. Find F(30,10001) mod 1000000007. """ factors = ut.factors(9) def fun_F(m, n): # 1*1 factors = ut.factors(m) pass if __name__ == '__main__': modulus = 1_000_000_007
def fun_F(m, n): # 1*1 factors = ut.factors(m) pass
def n_prime_factors(n, num_distinct_factors): def _check_prime_factors_multiply(num, prime_facts, new_prime_facts=None, prime_idx_to_exponent=0, exponent=2): product = ut.product( new_prime_facts) if new_prime_facts else ut.product(prime_factors) if product == num: return True # if smaller, for each of the prime factors, try increasingly higher powers UNTIL exceed elif product < num: if new_prime_facts: new_prime_facts = new_prime_facts[0:prime_idx_to_exponent] + [ new_prime_facts[prime_idx_to_exponent]**exponent, ] + new_prime_facts[prime_idx_to_exponent + 1:] else: new_prime_facts = prime_facts[0:prime_idx_to_exponent] + [ prime_facts[prime_idx_to_exponent]**exponent, ] + prime_facts[prime_idx_to_exponent + 1:] return _check_prime_factors_multiply(num, prime_facts, new_prime_facts, prime_idx_to_exponent, exponent + 1) elif product > num and prime_idx_to_exponent + 1 < len(prime_facts): # exceeded target number BUT still other return _check_prime_factors_multiply( num, prime_facts, new_prime_facts=None, prime_idx_to_exponent=prime_idx_to_exponent + 1, exponent=2) else: return False consecutive_ints = [] while len(consecutive_ints) < num_distinct_factors: factors = ut.factors(n) # filter for prime factors and factors != n prime_factors = [ fact for fact in factors if ut.is_prime(fact) if fact not in {1, n} ] if len(prime_factors) == num_distinct_factors: check_outcome = _check_prime_factors_multiply(n, prime_factors) if check_outcome: consecutive_ints.append(n) else: consecutive_ints = [] elif len(prime_factors) != num_distinct_factors and consecutive_ints: consecutive_ints = [] n += 1 return consecutive_ints
def _input_func(s): rets = [] for t in range(1, s, 2): if factors(s) & factors(t) == {1}: rets.append((a(s, t), b(s, t), c(s, t))) return rets
""" The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 2² × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? """ factors = ut.factors(644) prime_factors = [fact for fact in factors if ut.is_prime(fact)] def n_prime_factors(n, num_distinct_factors): def _check_prime_factors_multiply(num, prime_facts, new_prime_facts=None, prime_idx_to_exponent=0, exponent=2): product = ut.product( new_prime_facts) if new_prime_facts else ut.product(prime_factors) if product == num: return True # if smaller, for each of the prime factors, try increasingly higher powers UNTIL exceed
#!/usr/bin/python import os import sys import argparse #Auto-import parent module sys.path.insert(1, os.path.join(sys.path[0], '..')) import util #from pydmv import util parser = argparse.ArgumentParser(description="Show all factors of an integer") parser.add_argument("value", help="An integer") args = parser.parse_args() value = int(args.value) print("Factors of {} are {}".format(value, util.factors(value)))
def debug(x): print(x, util.factors(x), factorSum(x), isAbundant(x))
import numpy as np from util import factors """ Find sum of numbers not expressible as the sum of two abundant numbers """ TOP = 28124 abundants = [] nosum = np.ones(TOP, dtype='bool') # find the abundant numbers below TOP for i in range(12, TOP): if sum(factors(i)[:-1]) > i: abundants.append(i) abundants = np.array(abundants) # compute each pairwise sum of abundants # mark the corresponding number as disqualified for i in range(len(abundants)): if abundants[i] > TOP / 2: break s = abundants[i] + abundants[i:] hit = np.where(s < TOP) nosum[s[hit]] = False #sum up the numbers ind, = np.where(nosum) print ind.sum()
from util import factors print max(factors(600851475143))
#!/usr/bin/env python import util print(max(util.factors(600851475143)))
def first_triagle_with_divisors(n): triangle_number = 1; i = 1 while len(util.factors(triangle_number)) <= n: i += 1 triangle_number += i return triangle_number
from util import factors top = 10000 d = [0] * (top+1) for i in range(top): d[i] = sum(factors(i)[:-1]) result = 0 for i in range(1, top): if d[i] > i and d[i] < top and d[d[i]] == i: result += d[i] result += i print result
def answer(n): return max(factors(n))