def find_cyclic_polygonals(polygonals, used, number, first="octagon"): if number == 0 and digits(used[0][1])[0:2] == digits(used[-1][1])[2:4]: return used, True if not used: ps = polygonals[first] for p in ps: used.append((first, p)) used, found = find_cyclic_polygonals(polygonals, used, number - 1) if found: return used, True used.pop() return used, False last = used[-1][1] last_ending = digits(last)[2:4] for f, ps in polygonals.items(): if f not in {x[0] for x in used}: for new_ending in range(10, 100): new_polygonal = concatenate(last_ending + digits(new_ending)) if new_polygonal in ps and new_polygonal not in { x[1] for x in used }: used.append((f, new_polygonal)) used, found = find_cyclic_polygonals( polygonals, used, number - 1) if found: return used, True used.pop() return used, False
def is_pandigital(n): if n == 0: return False pd = euler.proper_div(n) if len(pd) == 1: return False bd = euler.digits(n) bda = [0] * 10 bda[0] = 1 for d in bd: if bda[d]: return False bda[d] = 1 for x in range(1, (len(pd)+1)//2): tda = bda.copy() for d in euler.digits(pd[x]): if tda[d]: continue tda[d] = 1 for d in euler.digits(n // pd[x]): if tda[d]: continue tda[d] = 1 if (sum(tda[1:]) == 9): return True return False
def valid(n): d = sorted(digits(n)) t = 2 m = t * n while sorted(digits(m)) == d: t += 1 m = t * n return t >= 7
def is_lychrel(n): i = 0 while i < 50: n += int(''.join(list(reversed(digits(n))))) if is_palindrome(digits(n)): return False i += 1 return True
def is_pand_multiple(n): for f in range(1,5): nd = euler.digits(n) basen = euler.num_from_digits(nd[-f:]) currn = basen nd = nd[:-f] while nd: currn += basen cnd = euler.digits(currn) if (nd[-len(cnd):] != cnd): break nd = nd[:-len(cnd)] if not nd: return True return False
def is_interesting(i): digits = euler.digits(i) sdigits = sum(digits) if sdigits == 1: return False p = math.log(i, sdigits) return i == (sdigits**math.ceil(p)) or i == (sdigits**math.floor(p))
def is_interesting(i): digits = euler.digits(i) sdigits = sum(digits) if sdigits == 1: return False p = math.log(i, sdigits) return i == (sdigits ** math.ceil(p)) or i == (sdigits ** math.floor(p))
def generate_interesting(): for n in range(1, 5000): for exp in range(2, 10): v = n**exp ds = digits(v) if len(ds) > 1 and sum(ds) == n: yield n, exp, v
def foo(n): res = [] for i in range(100, 1000): if i % n == 0: s = euler.digits(i) if len(set(s)) == 3: res.append(s) return res
def search(n): for i in itertools.count(1): d = euler.digits(i) if d * n > 9: return s = "".join(str(i * j) for j in range(1, n + 1)) if list(sorted(s)) == goal: yield s
def foo(n): res = [] for i in range(100,1000): if i % n == 0: s = euler.digits(i) if len(set(s)) == 3: res.append(s) return res
def itermasks(p): d = euler.digits(p) limit = 10**(d - 1) allmasks = itertools.product(range(2), repeat=d) for mask in itertools.islice(allmasks, 1, 2**d - 1): base = int("".join( (i if j == 0 else "0") for i, j in zip(str(p), mask))) imask = intmask(mask) yield (sum(candidates(base, imask, limit)), base, imask)
def is_trunc(n): if not euler.is_prime(n): return False d = euler.digits(n) for c in range(1,len(d)): lt = euler.num_from_digits(d[:-c]) rt = euler.num_from_digits(d[c:]) if not (euler.is_prime(lt) and euler.is_prime(rt)): return False return True
def digit_power_finder(p): nums = [] digit_max = 9**p d = 1 while digits(d * digit_max) > d: d += 1 for i in range(2, d * digit_max + 1): if digit_powers(i, p): nums.append(i) return nums
def prob30(): cnt = 0 # picking the upper bound here was guess work, but it makes # sense that you could prove this given some extra work for i in xrange(2, 7): for n in numbers_with_n_digits(i): digits = euler.digits(n) fifth_power_sum = sum([ d**5 for d in digits]) if fifth_power_sum == n: cnt += n return cnt
def prob30(): cnt = 0 # picking the upper bound here was guess work, but it makes # sense that you could prove this given some extra work for i in xrange(2, 7): for n in numbers_with_n_digits(i): digits = euler.digits(n) fifth_power_sum = sum([d**5 for d in digits]) if fifth_power_sum == n: cnt += n return cnt
from euler import digits, factorial print sum(digits(factorial(100)))
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F12, is the first term to contain three digits. What is the index of the first term in the Fibonacci sequence to contain 1000 digits? ''' from euler import fibonaccis, digits fibs = fibonaccis() for i, f in enumerate(fibs): if digits(f) == 1000: print(i) print(f) break
from euler import digits print sum(digits(2**1000))
def is_palindrome(n): ds = digits(n) return ds == list(reversed(ds))
#!/usr/bin/python import euler import math maxSum = sum(euler.digits(99**99)) max = [99, 99] for a in range(99, 1, -1): minB = int(math.floor((maxSum / 9) * math.log(10, a))) for b in range(99, minB, -1): s = sum(euler.digits(a**b)) if (s > maxSum): maxSum = s max = [a, b] print('maxSum = {}'.format(maxSum)) print('{}**{} = {}'.format(a, b, a**b))
def digit_factorial_sum(n): ds = digits(n) return sum([ factorial(d) for d in ds ])
def is_desc(n): ds = digits(n) return all([a >= b for a, b in zip(ds, ds[1:])])
#!/usr/bin/python import euler from operator import add from math import factorial print(reduce(add, euler.digits(factorial(100)), 0))
def is_pandigital(x): return sorted(digits(x)) == list(range(1,10))
#!/usr/bin/python import euler Power = 5 Sum = 0 for x in range(2, 9**Power * Power): if x == sum(map(lambda x: x**Power, euler.digits(x))): Sum += x print(Sum)
UpTo = 1000000 primes = euler.sieve(UpTo) circular = [0] * len(primes) def rotate(d): return d[-1:] + d[:-1] for x in range(0, len(primes)): if not primes[x]: continue if circular[x]: continue d = rotate(euler.digits(x)) rd = euler.num_from_digits(d) while rd != x: if not primes[rd]: break d = rotate(d) rd = euler.num_from_digits(d) if rd == x: d = rotate(d) rd = euler.num_from_digits(d) circular[x] = 1 while rd != x: circular[rd] = 1 d = rotate(d) rd = euler.num_from_digits(d)
import euler i = 2 x = 1 y = 1 while True: i += 1 z = x + y x = y y = z if euler.digits(z) == 1000: break print i
def odd_digits(d): digits = euler.digits(d) return [d%2==1 for d in digits]
def is_reversible(n): if n % 10 == 0: return False m = n + reverse_number(n) dm = digits(m) return all([ d % 2 == 1 for d in dm ])
#!/usr/bin/python import euler print(sum(euler.digits(1 << 1000)))
#!/usr/bin/python import euler OddFractions = [] def check_frac(n, d, bn, bd): global OddFractions if (bd == 0): return proper = n / float(d) improper = bn / float(bd) if abs(proper - improper) < 0.00000001: print('{} / {} = {} / {}'.format(n, d, bn, bd)) for n in range(10, 100): nd = euler.digits(n) for d in range(n + 1, 100): if (n == d): continue dd = euler.digits(d) if nd[0] == dd[0] and dd[0] != 0: check_frac(n, d, nd[1], dd[1]) if nd[1] == dd[0] and dd[0] != 0: check_frac(n, d, nd[0], dd[1]) if nd[0] == dd[1] and dd[1] != 0: check_frac(n, d, nd[1], dd[0]) if nd[1] == dd[1] and dd[1] != 0: check_frac(n, d, nd[0], dd[0])
def has_repeated_digit(n): dgts = digits(n) return len(dgts) > len(set(dgts))
def is_bouncy(n): ds = digits(n) asc = all([a <= b for a, b in zip(ds, ds[1:])]) desc = all([a >= b for a, b in zip(ds, ds[1:])]) return not (asc or desc)
def next_chain(x): square_digits = [d*d for d in euler.digits(x)] return sum(square_digits)
#!/usr/bin/python import euler from math import factorial facts = list(map(factorial, range(10))) Sum = 0 for n in range(3, 7 * factorial(9)): if n == sum(map(lambda x: facts[x], euler.digits(n))): print n Sum += n n += 1 print('Total sum: {}'.format(Sum))
# which has a solution of 7.43... # so we only need to consider up to 7-digit numbers from euler import digits factorials = {} def factorial(n): global factorials if n in factorials.keys(): return factorials[n] if n == 0 or n == 1: factorials[n] = 1 return 1 r = n * factorial(n - 1) factorials[n] = r return r digit_factorials = [factorial(x) for x in range(0, 10)] total = 0 for n in range(10, 10**7): dgs = digits(n) fac_sum = sum(digit_factorials[d] for d in dgs) if fac_sum == n: total += n print(total)
numbers["30"] = "thirty" numbers["40"] = "forty" numbers["50"] = "fifty" numbers["60"] = "sixty" numbers["70"] = "seventy" numbers["80"] = "eighty" numbers["90"] = "ninety" numbers["1000"] = "onethousand" numbers["00"] = "" numbers["0"] = "" keys = numbers.keys() for i in range(1, 1001): istr = str(i) digits = euler.digits(i) if i <= 20: suma.append(len(numbers[istr])) continue elif digits == 2: if istr in keys: suma.append(len(numbers[istr])) continue else: suma.append(len(numbers[istr[0]+'0']) + len(numbers[istr[1]])) continue
import sys sys.path.append("..") from euler import digits from fraction import Fraction c = 0 f = 1 for i in range(1000): f = (1 + Fraction(1, 1 + f)).reduce() num_digit = len(digits(f.numerator)) den_digit = len(digits(f.denominator)) if num_digit > den_digit: c += 1 #print(i, ": ", f, " count: ", c) print(c)
# 10 * n * x + m * n == 10 * m * x + m * n ---> m == n (i.e., the fraction is 1) or x == 0 (trivial case) # # Now the ones place: # (10 * m + x) / (10 * n + x) == m / n # 10 * m * n + x * n == 10 * m * n + x * m --> m == n (i.e., the fraction is 1) or x == 0 (trivial case) # So the repeated digit is opposite places # Also note that the denominator must be more than the numerator from euler import digits, product from math import gcd # Find the numerators and denominators of the fractions that satisfy this fracs = set() for num in range(10, 100): num_digs = digits(num) for den in range(num + 1, 100): den_digs = digits(den) # Skip the 'trivial' cases if num_digs[0] == 0 and den_digs[0] == 0: continue # If we have matching digits if num_digs[0] == den_digs[1]: if (num * den_digs[0] == den * num_digs[1]): fracs.add((num_digs[1], den_digs[0])) if num_digs[1] == den_digs[0]: if (num * den_digs[1] == den * num_digs[0]): fracs.add((num_digs[0], den_digs[1]))
import sys sys.path.append("..") from fraction import Fraction from euler import sieve, digits upper = 100 primes = sieve(upper) valid = [] for den in range(10, upper): if den % 10 != 0: for num in range(10, den): q = Fraction(num, den) num_digits = digits(num, convert=True) den_digits = digits(den, convert=True) if len(set(num_digits) & set(den_digits)) == 1: same = (set(num_digits) & set(den_digits)).pop() num_digits.remove(same) den_digits.remove(same) num_fake = num_digits[0] den_fake = den_digits[0] fake = Fraction(num_fake, den_fake) if q == fake: valid.append(q) result = 1 for q in valid: result *= q.reduce() print(result.reduce().denominator)
from euler import digits print sum(digits(2 ** 1000))