def fast_sum_divisors(n): product = 1 for factor, power in prime_factors(n, 'dictionary').items(): term = 1 for i in range(1, power + 1): term += factor**i product *= term return product - n
def phi(n, limit=None): idx = n - 1 f = prime_factors(n, 'set') if limit and len(f) > limit: return n // 2 for i in range(len(f)): multiplier = [-1, 1] c = c_prod(f, i + 1) for x in c: idx += (n // x - 1) * multiplier[i % 2] return idx
def m_primes(n): f = set(prime_factors(n)) m = [] for i in range(1, n): for p in f: if not i % p: break else: m.append(i) # if not set(prime_factors(i)) & f: return m
def fracs(n): factors = prime_factors(n) start = ceil(n / 3) end = n // 2 for i in range(start, end+1): if i == 1: yield 1 continue if i in primes and n % i: yield i continue for f in factors: if i % f == 0: break else: yield i
def phi(n): f = prime_factors(n) if len(f) == 1: cache[n] = n-1 return n-1 family = product(set(f)) if family > 1 and family in cache: cache[n] = cache[family] * n // family return cache[n] f = set(f) idx = n - 1 for i in range(len(f)): multiplier = [-1, 1] c = c_prod(f, i+1) for x in c: idx += (n//x - 1) * multiplier[i%2] cache[n] = idx return idx
def first_tri_num_with_more_than_n_factors(n): for tri_number in triangular(None): if 2**len(prime_factors(tri_number)) >= n: if len(all_factors(tri_number, include_self=True)) > n: return tri_number
from primesandfactors import prime_factors from generators import miller_rabin_range from benchmark import benchmark from numpy import array lowest_factorial_multiple = lambda n: max( ( f(v, k) for k, v in prime_factors(n, 'dictionary').items() ) ) def f(n, b): m = n total = 0 while n > 0: i, e = 0, 0 while True: x = b**e if i + x > n: break i += x e += 1 n -= i total += x return total @benchmark() def run(n): done = array( [True] * (n+1) ) total = count = 0 for prime in miller_rabin_range(n+1): y = min(n//prime, prime) total += prime * (y-1) for x in range(1, y): done[prime*x] = False count += 1
def run2(num): n = 1 for p in primes2(num): n *= p ph = phi(n) print(p, ph, prime_factors(ph))