def get_factors(x): global cached_factors if x in cached_factors: return cached_factors[x] facs = factors(x) cached_factors[x] = facs return facs
def totient(n): factors = euler.factors(n) coprime = [1 for i in range(n)] coprime[0] = 0 for f in factors: for i in range(f, n, f): coprime[i] = 0 return sum(coprime)
# #################################################### # Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). # If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. # For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. # Evaluate the sum of all the amicable numbers under 10000. #################################################### from euler import factors divisor_sums = {} max_value = 10000 for i in range(2, max_value): divisor_sums[i] = int(sum(factors(i)) - i) amicable_numbers = set() for i in range(2, max_value): j = divisor_sums[i] # If we've already found this value from its partner if i in amicable_numbers: continue if j not in divisor_sums.keys(): divisor_sums[j] = int(sum(factors(j)) - j) if divisor_sums[j] == i and i != j: if j > 0 and j < max_value: amicable_numbers.add(j)
# Mar 29, 2016 import euler n, new_num = 1, 1 while len(euler.factors(new_num)) < 500: n += 1 new_num = int((n * (n + 1)) / 2) print "Ans:", new_num
#################################################### # The prime factors of 13195 are 5, 7, 13 and 29. # What is the largest prime factor of the number 600851475143? #################################################### from euler import is_prime, factors # Get the factors of x def get_factors(x): global cached_factors if x in cached_factors: return cached_factors[x] facs = factors(x) cached_factors[x] = facs return facs # Do the calculation # To avoid redundant calculations, we cache answers cached_factors = {} facs = sorted(factors(600851475143), reverse=True) for x in facs: if is_prime(x): print(x) break
def f(n): # return n/float(totient(n)) return n / float(totient2(n)) def main(maxv): best = 2 f_best = 0 for n in range(390390, maxv): if euler.isprime(n) == False: f_n = f(n) if f_n > f_best: print 'possible', n, f_n best = n f_best = f_n if n % 1000 == 0: print '.' #n,f(n),'best',best,f_best #main(1000000) for i in range(2, 1000000): if euler.isprime(i) == False: if len(euler.factors(i)) > 50: if f(i) > 5.1: print i, len(euler.factors(i)), f(i) #v = totient2(i) #if i % 1000 == 0: # print i,v,totient(i),i/float(v),len(euler.factors(i))
#!/usr/bin/python import euler print(euler.factors(600851475143))
primeFactors = euler.primeFactors(230, primes) assert primeFactors == primeFactors0 primeFactors0 = [2, 2, 3, 3] primeFactors = euler.primeFactors(36, primes) assert primeFactors == primeFactors0 primePowerFactorization0 = {2: 2, 3: 2} primePowerFactorization = euler.primePowerFactorization(primeFactors) assert primePowerFactorization0 == primePowerFactorization assert (euler.descendingFactorial(5, 2) == 20) assert (euler.descendingFactorial(5, 3) == 60) assert (euler.ascendingFactorial(5, 2) == 30) assert (euler.ascendingFactorial(5, 3) == 210) assert (euler.descendingFactorial(5, -2) == 1 / 42) assert (euler.descendingFactorial(5, -3) == 1 / 336) assert (euler.ascendingFactorial(5, -2) == 1 / 12) assert (euler.ascendingFactorial(5, -3) == 1 / 24) assert (euler.factors(20, []) == [1, 2, 4, 5, 10, 20]) assert (euler.appendDigits(37465, []) == [3, 4, 5, 6, 7]) assert (euler.appendDigitsUnsorted(37465, []) == [3, 7, 4, 6, 5]) primes = euler.readPrimes() assert (primes[0:20] == [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 ])
def testFactors(self): self.assert_(euler.factors(24) == set([2,3,4,6,8,12]))
# April 15, 2016 import euler amicable_numbers = list() for n in range(1, 10000): b = sum(euler.factors(n)[:-1]) if n != b and sum( euler.factors(b)[:-1]) == n and n not in amicable_numbers: amicable_numbers.append(n) if b <= 10000: amicable_numbers.append(b) print sorted(amicable_numbers) print '\n' + str(sum(amicable_numbers))
#!/usr/bin/python import euler p = 0 n = 1 while p < 10001: n += 1 if (n % 2 == 0) or (n % 3 == 0) or (n % 5 == 0): continue if len(euler.factors(n)) == 1: p += 1 print(n)
# 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? #################################################### from euler import factors def triangle_number(n): return n * (n + 1) // 2 n = 1 while True: tri_num = triangle_number(n) facs = factors(tri_num) if len(facs) > 500: break n += 1 print(tri_num)
#!/usr/bin/python import euler total_factors = [0] * 21 for n in range(20, 2, -1): f = euler.factors(n) while f: x = f[0] c = f.count(x) total_factors[x] = max(total_factors[x], c) f = [e for e in f if e != x] prod = 1 for x in range(2, 21): prod *= (x**total_factors[x]) print(prod)
def primeFactors(n): s = set() for f in euler.factors(n): if euler.isprime(f): s.add(f) return s
#!/usr/bin/python import euler tn = 1 c = 1 ld = 1 while ld < 500: c += 1 tn += c f = euler.factors(tn) if (len(f) > 8): d = euler.divisors(f) ld = len(d) print(c, tn, ld)
import euler d = [sum(list(euler.factors(i))) + 1 for i in range(10001)] s = set() for a in range(10001): try: b = d[a] if d[b] == a and a != b: s.add((min(a, b), max(a, b))) except: pass lst = [] for a, b in s: lst.append(a) lst.append(b) print lst print sum(lst)
def divisors(n): lst = [1] + list(euler.factors(n)) lst.sort() return lst
import euler for i in range(10,100): for j in range(10,100): if i != j and ((i%10!=0) and (j%10!=0)): ifactors = euler.factors(i) jfactors = euler.factors(j) if ifactors.intersection(jfactors): print i,'/',j,' ',
def factors(n): s = euler.factors(n) s.add(1) s.add(n) return list(s)
def is_abundant(n): facs = factors(n) div_sum = sum(facs) - n return div_sum > n
def wrapper(*args): s = func(*args) if is_prime(args[0]): return set([1]) return s.difference(args) return wrapper def sum_it(val): if len(val) < 2: return 1 return reduce(lambda x, y: x + y, val) factors = remove_last(factors) # print sorted(factors(220)) # print reduce(lambda x, y: x+y, sorted(factors(220))) # print reduce(lambda x, y: x+y, sorted(factors(284))) sum = 0 for num in range(2, 10000): a = sum_it(factors(num)) # print a # print factors(a) b = sum_it(factors(a)) # print b if num == b and num != a: print "here", num sum += num print "the answer is: ", sum
# April 17, 2016 import euler limit = 28123 abundant_numbers = list() for n in xrange(12, limit + 1): if sum(euler.factors(n)[:-1]) > n: abundant_numbers.append(n) all_sums = list() for j in range(len(abundant_numbers) - 1): for k in range(j, len(abundant_numbers) - 1): if abundant_numbers[j] + abundant_numbers[k] > limit: break all_sums.append(abundant_numbers[j] + abundant_numbers[k]) all_sums = sorted(list(set(all_sums))) ans, k = 0, 0 for n in range(1, limit + 1): if n not in all_sums[k:]: ans += n if n > all_sums[k] and k < len(all_sums) - 1: k += 1 print ans