def upper_bound_generating_set(n): """ Estimate an upper bound for the integer k such that any group of order n has a generating subset of size at most k. Estimated by prime factorising n and adding together the exponents of each prime. The correctness of this bound may be reasoned as follows: Let G be a group of order n. Let g_0 be the identity 1 in G, and inductively, for each i, set H_i = <g_1, ... g_i>. Choose a g_{i + 1} not in H_i. Then the order of H_{i + 1} is a proper multiple of the order of H_i, by Lagrange's theorem. Say that r is the least integer such that H_r = G. We can write |G| = (|H_r| / |H_{r - 1}|) (|H_{r - 1}| / |H_{r - 2}|) ... (|H_2| / |H_1|) (|H_1| / |H_0|) ie |G| is a product of r nonunit integers, by construction. By considering prime factorisations it follows that r is at most the number of prime factors that |G| has, counted with multiplicity. For p-groups, groups of order 2p, and probably some other classes, this bound can be shown to be tight. Sadly we know that the only group of order 15 is Z/15Z, which is generated by one element. """ return sum(1 for _ in prime_factors(n))
def d(n): if n in cache: return cache[n] f = factors.factors(primes.prime_factors(n,prime_list)) f.remove(n) cache[n] = sum(f) return sum(f)
def answer(): target = 15499 / 94744 start = 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 for n in count(start, start): factors = prime_factors(n).items() r = totient(factors) / (n - 1) if r < target: return n
def main(): current_sieve_limit = 100000 sieve = sieve_of_eratosthenes(current_sieve_limit) n = 2*3*4*5 - 1 while True: n += 1 # Re-compute the sieve if n > current_sieve_limit: current_sieve_limit *= 2 sieve = sieve_of_eratosthenes(current_sieve_limit) if (len(prime_factors(n, sieve)) == 4 and len(prime_factors(n+1, sieve)) == 4 and len(prime_factors(n+2, sieve)) == 4 and len(prime_factors(n+3, sieve)) == 4): print(n) break
def answer(): consecutive = [] number = 646 while len(consecutive) < 4: number += 1 if len(primes.prime_factors(number)) == 4: consecutive.append(number) else: consecutive = [] return consecutive[0]
def least_common_multiple(m = []): nums = [] mpfs = [] for n in m: nums.append(primes.prime_factors(n)) # Build the list for g in nums: for f in set(g): while mpfs.count(f) < g.count(f): mpfs.append(f) return primes.list_product(mpfs)
def main(): consecutive_count = 0 for i in range(1000,1000000): factors = prime_factors(i) if len(set(factors)) == 4: consecutive_count += 1 else: consecutive_count = 0 if consecutive_count == 4: for j in range(i-3, i+1): print('%s: %s' % (j, factors)) return
def factors(number): """ Given a number, return a list of all factors as (prime, exponent) tuples """ factors = {} try: for factor in prime_factors(number): try: factors[factor] += 1 except KeyError: factors[factor] = 1 except TypeError: # prime_numbers() is None pass # No factors but itself if len(factors) == 0: return [(number, 1)] return [(prime, exp) for (prime, exp) in factors.items()]
def p69(n): start = timer() et = {} phis = {} for i in primorial(n): et[i] = list(set(prime_factors(i))) for i in et: phi = i for x in et[i]: phi *= (1 - 1 / float(x)) phis[i] = [phi, float(i) / phi] v = [x[1] for x in phis.values()] k = list(phis.keys()) kmax = k[v.index(max(v))] print(kmax, phis[kmax]) print('Elapsed time: ', timer() - start, 's')
def p69(n): start=timer() et={} phis={} for i in primorial(n): et[i]=list(set(prime_factors(i))) for i in et: phi=i for x in et[i]: phi*=(1-1/float(x)) phis[i]=[phi,float(i)/phi] v=[x[1] for x in phis.values()] k=list(phis.keys()) kmax= k[v.index(max(v))] print (kmax,phis[kmax]) print ('Elapsed time: ',timer()-start,'s')
def main(): relevant_ints = range(1, 21) prime_factors = [primes.prime_factors(n) for n in relevant_ints] # num_factors: { prime factor : number of occurrences }, e.g. 9 would consist of {3: 2} num_factors = {} for pf in prime_factors: pf_count = Counter(pf) for key, value in pf_count.items(): # check if the num_factors already include this specific prime factorization # e.g. the prime factorization of 20 (2,2,5) and 18 (2,3,3) the prime factorization of 12 (2,2,3). if key in num_factors: if num_factors[key] < value: num_factors[key] = value else: num_factors[key] = value product = 1 for key, value in num_factors.items(): product *= (key**value) return product
assert (is_prime(13)) assert (is_prime(17)) assert (is_prime(23)) assert (is_prime(29)) assert (is_prime(31)) assert (is_prime(37)) assert (not is_prime(-1)) assert (not is_prime(0)) assert (not is_prime(4)) assert (not is_prime(6)) assert (not is_prime(9)) assert (not is_prime(15)) assert (not is_prime(25)) with timeit("prime_factors test suite"): assert (list(prime_factors(2)) == [2]) assert (list(prime_factors(3)) == [3]) assert (list(prime_factors(5)) == [5]) assert (list(prime_factors(2 * 3 * 5 * 7 * 11 * 13)) == [2, 3, 5, 7, 11, 13]) assert (list(prime_factors(2 * 2 * 3 * 3 * 5 * 5 * 7 * 7)) == [2, 2, 3, 3, 5, 5, 7, 7]) with timeit("all_factors test suite"): assert (all_factors(512) == {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}) assert (all_factors(2 * 3 * 5) == {1, 2, 3, 5, 2 * 3, 2 * 5, 3 * 5, 2 * 3 * 5}) with timeit("is_prime perf test"): assert (is_prime(2**61 - 1))
"""Solve problem 12 of Project Euler and store the answer in answer.""" from primes import prime_factors target = 500 temp, divisors, i = 0, 0, 1 while divisors <= target: temp += i i += 1 divisors, factors = 0, prime_factors(temp) for j in range(len(factors)): if j == 0: divisors += 1 increase = 1 elif factors[j] == factors[j - 1]: divisors += increase elif factors[j] != factors[j - 1]: divisors, increase = 2*divisors + 1, divisors + 1 divisors += 1 answer = temp
# numbers from 1 to 20? import primes def is_subset(la, lb): sa = set(la) #sb = set(lb) for i in sa: if la.count(i) > lb.count(i): return False return True def least_common_multiple(m = []): nums = [] mpfs = [] for n in m: nums.append(primes.prime_factors(n)) # Build the list for g in nums: for f in set(g): while mpfs.count(f) < g.count(f): mpfs.append(f) return primes.list_product(mpfs) p8 = primes.prime_factors(8) p16 = primes.prime_factors(16) print(least_common_multiple(list(range(1, 21))))
def divisors(n): pf = prime_factors(n) divisors = divisors_from_factorization(pf) return list(divisors)
""" Problem : What is the largest prime factor of the number 600851475143 ? Performance time: ~0.0014s """ from timer import timer from primes import prime_factors timer.start() print(max(prime_factors(600851475143))) timer.stop()
#!/usr/bin/env python3 #https://projecteuler.net/problem=5 import primes prime_list = primes.primes(20) factors = {} for i in range(2, 21): tmp = primes.prime_factors(i, prime_list) tmp = { x: tmp.count(x) for x in set(tmp) } for j in tmp.keys(): if tmp[j] > factors.get(j, 0): factors[j] = tmp[j] ans = 1 for i in factors: ans *= (i ** factors[i]) print(ans)
def main(): print(prime_factors(num)[-1])
#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? import primes import factors def triangle_generator(): tmp = 1 while True: yield tmp * (tmp+1) // 2 tmp += 1 prime_list = primes.primes(1000000) for t in triangle_generator(): tmp = factors.factors(primes.prime_factors(t, prime_list)) if len(tmp) > 500: print(t) exit()
def count_factors(x): pf = prime_factors(x) n = 1 for prime, count in pf: n *= (count+1) return n
def test_prime_factors(self): self.assertEqual(primes.prime_factors(15), [3, 5]) self.assertEqual(primes.prime_factors(460), [2, 2, 5, 23]) self.assertEqual(primes.prime_factors(13195), [5, 7, 13, 29])
def check_abundant(n): pf = factors.proper_factors(primes.prime_factors(n, prime_list)) return sum(pf) > n
import primes print(primes.isprime(18)) print(primes.all_primes(18)) print(primes.prime_factors(18))
def test_prime_factors(self): self.assertEqual(tuple(prime_factors(2)), (2, )) self.assertEqual(tuple(prime_factors(6)), (2, 3)) self.assertEqual(tuple(prime_factors(2000)), (2, 2, 2, 2, 5, 5, 5)) self.assertEqual(tuple(prime_factors(2017)), (2017, )) self.assertEqual(tuple(prime_factors(2021)), (43, 47))
def test_prime_factors(): print('test_prime_factors') assert prime_factors(2) == [2] assert prime_factors(12) == [2, 2, 3]
from time import process_time from primes import prime_factors print(process_time()) print(prime_factors(600851475143)[-1]) print(process_time())
""" Problem : How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d ≤ 12,000? Performance time : ~3.8s """ from timer import timer from primes import prime_factors timer.start() #mark all possible denominators with their prime factors compositeness = {n: set(prime_factors(n)) for n in range(12001)} answer = 0 for d in range(5, 12001): d_factors = compositeness[d] for n in range(d // 3, d // 2): n_factors = compositeness[n] if not n_factors & d_factors: answer += 1 print(answer) timer.stop()