def cycle(i): factors = prime_factors(i) # Filter out all 2s and 5s i = 1 for p in factors: if p != 2 and p != 5: i = i * p if i == 1: return 0 digits = 1 n = 9 while n % i != 0: n = n * 10 + 9 digits = digits + 1 return digits
#!/usr/bin/env python3 ''' Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ''' from sys import argv from prime import prime_factors if __name__ == '__main__': try: target = int(argv[1]) except IndexError: target = 600851475143 print(max(prime_factors(target)))
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? """ from collections import defaultdict from sys import argv from prime import prime_factors if __name__ == "__main__": try: target = int(argv[1]) except IndexError: target = 20 powers = defaultdict(int) for n in range(2, target + 1): prime_powers = prime_factors(n) for k in prime_powers: powers[k] = max(powers[k], prime_powers[k]) total = 1 for k in powers: total *= k ** powers[k] print(total)
def num_divisors(x): # Based on http://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors factors = prime_factors(x) return reduce(lambda x, y: x * (y + 1), factors.values(), 1)
import prime def com(n,k): return factorial(n)/(factorial(n-k)*factorial(k)) def com(n,k): return factorial(n)/(factorial(n-k)*factorial(k)) b = (0,0) e = (2,2) p = [(0,1),(0,1),(1,0),(1,0)] r = set([pp for pp in permutations(p)]) #print(r) print(len(r)) # Too big #p = [(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0),(1,0)] #r = set([pp for pp in permutations(p)]) #print(r) #print(len(r)) print( com(52,5) ) print( com(500,5) ) res = com(2,1)*com(10,1)*com(2,1)*com(10,1)*com(2,1)*com(9,1)*com(2,1)*com(9,1)*com(2,1)*com(8,1)*com(2,1)*com(8,1)*com(2,1)*com(7,1)*com(2,1)*com(7,1)*com(2,1)*com(6,1)*com(2,1)*com(6,1)*com(2,1)*com(5,1)*com(2,1)*com(5,1)*com(2,1)*com(4,1)*com(2,1)*com(4,1)*com(2,1)*com(3,1)*com(2,1)*com(3,1)*com(2,1)*com(2,1)*com(2,1)*com(2,1) print( res ) print( prime.prime_factors(res / 137846528820) ) print( prime.prime_factors(137846528820) ) # unsolved
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. What is the first of these numbers? ''' from itertools import count from prime import prime_factors if __name__ == '__main__': sequence = 0 for number in count(1): if len(prime_factors(number)) == 4: sequence += 1 if sequence == 4: print(number - 3) break else: sequence = 0
from prime import get_primes, prime_factors max_prime = 300000 primes = get_primes(max_prime) i = 0 while i < max_prime-3: if len(prime_factors(i+3, primes)) == 4: if len(prime_factors(i+2, primes)) == 4: if len(prime_factors(i+1, primes)) == 4: if len(prime_factors(i, primes)) == 4: print(i) else: i += 1 else: i += 2 else: i += 3 else: i += 4
Each tuple is create by taking a digit from :param digits: and removing the first instance of that digit from both i and j. If this would cause either i or j to be 0, then that tuple is skipped. """ return filter( lambda x: x[0] > 0 and x[1] > 0, ((int(str(i).replace(d, '', 1)), int(str(j).replace(d, '', 1))) for d in digits)) fract = [1, 1] for i in range(10, 100): i_digits = set(str(i)) for j in range(i + 1, 100): if i % 10 == 0 and j % 10 == 0: continue j_digits = set(str(j)) inter = i_digits.intersection(j_digits) for n, d in generate_test_fractions(i, j, inter): if isclose(i / j, n / d): print("{}/{} == {}/{}".format(i, j, n, d)) fract[0] *= n fract[1] *= d fract = cancel_common_factors(prime_factors(fract[0]), prime_factors(fract[1])) print("Denominator of products: {}".format(expand_factorization(fract[1]))) print("Execution time: {}".format(time.time() - start_time))
divisibility_target = 500 def merge_factors(factorization_1, factorization_2): """Requires parameters in format returned by prime.prime_factors.""" factors = set(factorization_1).union(factorization_2) return { k: factorization_1.get(k, 0) + factorization_2.get(k, 0) for k in factors } # the first constiuent of the closed form of a triangle number: n(n+1)/2 n = 1 n_factors = dict() next_factors = prime_factors(n + 1) factor_count = 1 while factor_count <= divisibility_target: n += 1 n_factors = next_factors next_factors = prime_factors(n + 1) merged = merge_factors(n_factors, next_factors) # There will always be an extra factor of 2 (see closed form), which is divided out merged[2] -= 1 # The actual number of factors (prime and otherwise) is given by this product factor_count = reduce(mul, (v + 1 for v in merged.values()))
from prime import prime_factors distinct_primes = [1, 1, 1, 1] n = 647 still_checking = True while still_checking: distinct_primes[0] = distinct_primes[1] distinct_primes[1] = distinct_primes[2] distinct_primes[2] = distinct_primes[3] s = set(prime_factors(n)) distinct_primes[3] = len(s) if distinct_primes[0] > 3 and distinct_primes[1] > 3 and distinct_primes[2] > 3 and distinct_primes[3] > 3: still_checking = False print(n) else: n += 1
#/bin/python # http://projecteuler.net/problem=3 # The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? import prime fact = prime.prime_factors(600851475143) print( fact ) # [71, 839, 1471, 6857] print( max(fact) ) # 6857
def main(): print(max(prime_factors(600851475143)))
import prime print(prime.prime_factors(600851475143))
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? ''' from collections import defaultdict from sys import argv from prime import prime_factors if __name__ == '__main__': try: target = int(argv[1]) except IndexError: target = 20 powers = defaultdict(int) for n in range(2, target + 1): prime_powers = prime_factors(n) for k in prime_powers: powers[k] = max(powers[k], prime_powers[k]) total = 1 for k in powers: total *= k ** powers[k] print(total)