def main(): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # print primes.getPrimeFactors(4) # print primes.getPrimeFactors(6) # print primes.getPrimeFactors(59) # print primes.getPrimeFactors(13195) print primes.getPrimeFactors(600851475143)[-1]
def findNumsWithDumbReduction(): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - reducible_num = [] reducible_den = [] for den in xrange(11, 100): if den%10 == 0: continue for num in xrange(11, den): if num%10 == 0: continue reduced = doDumbReduction(num, den) red_frac = float(reduced['num'])/reduced['den'] if float(num)/den == red_frac: reducible_num.append(reduced['num']) reducible_den.append(reduced['den']) print 'reduced nums: %s' % reducible_num print 'reduced denoms: %s' % reducible_den red_product_num = reduce(operator.mul, reducible_num) red_product_den = reduce(operator.mul, reducible_den) print 'product of reduced nums: %d' % red_product_num print 'product of reduced denoms: %d' % red_product_den print 'product of reduced fracs: %f' % (float(red_product_num)/red_product_den) primes_list = primes.primeSeive(red_product_num) print primes_list prime_factors_num = collections.Counter( primes.getPrimeFactors( red_product_num , primes_list) ) prime_factors_den = collections.Counter( primes.getPrimeFactors( red_product_den , primes_list) ) common_prime_factors = list((prime_factors_num & prime_factors_den).elements()) for p in common_prime_factors: red_product_num /= p red_product_den /= p print 'Fully reduced product of special fractions is %d/%d = %f' % ( red_product_num , red_product_den , ( float(red_product_num)/red_product_den) )
def sumOfDivisors(num, primes_list): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if num == 0 or num == 1: return 0 prime_factors = collections.Counter(primes.getPrimeFactors(num, primes_list)) sum_of_divisors = 1 for pf in prime_factors: temp = 0 for i in xrange(prime_factors[pf] + 1): temp += pf ** i sum_of_divisors *= temp return sum_of_divisors - num